• Published
  • 1 min

Paging of search result

<p>Are&nbsp;you using EPiServer CMS 5 and need&nbsp;to present the search result with paging functionality? Take a look at the code below&nbsp;:)</p>

Are you using EPiServer CMS 5 and need to present the search result with paging functionality? Take a look at the code below :)

Aspx
You start out with the default search page provided by the manager installation. To create the paging functionality, you need to make some small adjustments to the default searchpage code. In the aspx page, you replace the default <asp:repeater code with a control which supports paging. I chose the EPiServer PageList control:

<EPiServer:PageList ID="searchList" PageLinkProperty="SearchPage" Paging="true" PagesPerPagingItem='<%# ItemsPrPage %>' runat="server" Visible="false"
    <HeaderTemplate>
        <h2>
            <asp:Literal ID="Literal1" runat="server" Text="<%$ Resources: EPiServer, search.searchresult %>" /></h2>
        <ol>
            <br />
    </HeaderTemplate>
    <ItemTemplate>
        <li class="searchResult"><a class="linkmarker" href="<%#EPiServer.UriSupport.AddLanguageSelection((string)DataBinder.Eval(Container.DataItem, "LinkURL"), (string)DataBinder.Eval(Container.DataItem, "LanguageBranch")) %>">
            <%# DataBinder.Eval(Container.DataItem, "PageName")%></a>
            <p>
                <%# GetPreviewText(Container.DataItem) %>
            </p>
            <i><span id="Span1" class="dateTime" runat="server">
                <%# DataBinder.Eval(Container.DataItem, "Changed", "{0:g}")%>
            </span></i></li>
    </ItemTemplate>
    <FooterTemplate>
        </ol></FooterTemplate>
</EPiServer:PageList>

Codebehind

In the codebhind file, you need to change the protected override void OnLoad(....) method. You need to add the following code at the end of the method:
searchList.DataSource = SearchDataSource.Select(DataSourceSelectArguments.Empty);
searchList.DataBind();

In addition, you need to replace all references to the SearchResult control, with the reference to your new searchList control. Finally, you have to create the ItemsPrPage property. Here is an example of how to implement it:

private int _itemsPerPage = -1;
public int ItemsPrPage
        {
            get
            {
                if(_itemsPerPage <= 0)
                {
                    if (IsValue("ItemsPerPage"))
                    {
                        int i = -1;
                        Int32.TryParse(CurrentPage["ItemsPerPage"].ToString(), out i);
                        if (i >= 0)
                        {
                            _itemsPerPage = i;
                        }
                    }
                    else
                        _itemsPerPage = 10;
                }
                return _itemsPerPage;
            }
            set { _itemsPerPage = value; }
        }