DataPager控件使用Sitecore布局URL代替项目URL [英] DataPager controls use Sitecore layout URL instead of item URL

查看:114
本文介绍了DataPager控件使用Sitecore布局URL代替项目URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在Sitecore 6.3.1网站上实现搜索结果页,我在 / sitecore / content / Home / Search 中创建了一个内容项,并添加了Search Results子布局到其演示控件。子布局使用ListView显示搜索结果,并使用DataPager处理分页。

To implement a search results page on a Sitecore 6.3.1 site, I created a content item at /sitecore/content/Home/Search and added a Search Results sublayout to its presentation controls. The sublayout uses a ListView to display the search results and a DataPager to handle pagination.

此处摘录自 Search Results.ascx


<asp:ListView ID="SearchResults" runat="server">
  <LayoutTemplate>
    <asp:DataPager ID="Pager" QueryStringField="page" runat="server">
      <Fields>
        <asp:NumericPagerField ButtonCount="10" />
      </Fields>
    </asp:DataPager>

    <asp:Placeholder ID="itemPlaceholder" runat="server" />
  </LayoutTemplate>

  ...
</asp:ListView>

DataPager的 QueryStringField 参数设置为非空值。

Note that the DataPager's QueryStringField parameter is set to a non-empty value.

呈现子布局时,搜索结果分页控件正确显示。但是,分页超链接转到错误的URL。他们会链接到 layout 的URL,而不是转到页面URL。

When the sublayout is rendered, the search results and pagination controls appear correctly. However, the pagination hyperlinks go to the wrong URL. Instead of going to the page URL, they link to the layout's URL.

例如,如果用户单击以下链接,第2页,人们希望他的浏览器转到例如 http:// www.example.com/Search.aspx?query=xyz&page=2 。但是他的浏览器实际上链接到 http:// www .example.com / layouts / Generic%20Browser%20Layout.aspx?query = xyz& page = 2

For example, if the user clicks on the link for page 2, one would expect his browser to go to e.g., http://www.example.com/Search.aspx?query=xyz&page=2. But his browser actually links to http://www.example.com/layouts/Generic%20Browser%20Layout.aspx?query=xyz&page=2.

DataPager在哪里获取虚假URL ,以及如何解决此问题?

Where is the DataPager getting the bogus URL, and how do I fix this?

推荐答案

这是我最终选择的解决方案。它不是很漂亮,但是确实可以工作:

Here's the solution I ultimately went with. It's not pretty, but it does work:


/// <summary>
///   Fixes any HyperLinks that point to the layout .aspx file to point to
///     the Sitecore context item.
/// </summary>
/// <param name="control">
///   The control to fix (its child controls will be processed).
/// </param>
protected void FixLayoutHyperLinks(Control control)
{
  var currentPath = LinkManager.GetItemUrl(Sitecore.Context.Item);
  foreach (Control c in control.Controls)
  {
    foreach (Control d in c.Controls)
    {
      if (d is HyperLink)
      {
        var link = (HyperLink)d;

        /* Change just the path of the existing URL.
         * @see http://stackoverflow.com/questions/5276324/modifying-just-the-path-part-of-a-hyperlinks-navigateurl-in-c/5276375#5276375
         */
        var url = new UriBuilder(Request.Url.Host + link.NavigateUrl);
        url.Path = currentPath;

        /* For consistency (and because ASP.Net will strip the leading
         *  "http://" during PreRender), do not add the hostname/schema to
         *  the resulting URI.
         * 
         * @see http://sobot-software.blogspot.com/2009/02/asphyperlink-navigateurl-problem.html
         */
        link.NavigateUrl = url.Uri.PathAndQuery;
      }
    }
  }
}

我这样使用它:


private void Page_Load(object sender, EventArgs e)
{
  ...

  var Pager = MyListView.FindControl("Pager") as DataPager;
  FixLayoutHyperLinks(Pager);
}

这篇关于DataPager控件使用Sitecore布局URL代替项目URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆