+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Paging in Search Result

Paging in Search Result

12 posts, 1 answered
  1. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    10 Aug 2010
    Link to this post
    Hey,
    I want to enable paging at the top as well as the bottom of the page for search result. There is a control by name sfWeb:Pager having ID="pager1". This displays the paging at the bottom of the page. I require this at the top of the page as well. Please let me know how this can be achieved.

    Regards,
    Siddesh Kapadi

     

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    10 Aug 2010
    Link to this post
    Hi Siddesh Kapadi,

    You have to create a custom control that inherits from SearchResults and there implement a container with control reference to the second pager you have. Then override CrateChildControls of the derivate and bind the second pager to search results datasource.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    11 Aug 2010
    Link to this post
    Hey,
    Please can you let me know how can I implement the container with control reference to the second pager and also how can I get the search result datasource. Please help me in this as I am stuck up.


    Regards,
    Siddesh Kapadi
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    11 Aug 2010
    Link to this post
    Hello Siddesh Kapadi,

    Here is a sample code that shows how to create a custom container for the second pager

    protected class PagerContainer : GenericContainer<SearchResult>
          {
             
              public PagerContainer ( SearchResult owner )
                  : base ( owner )
              {
              }
            
     
     
              private Pager pager2;
     
              public Pager Pager2
              {
                  get
                  {
                      if (this.pager2 == null)
                          this.pager1 = base.FindRequiredControl<Pager>("pager2");
                      return this.pager2;
                  }
              }
          }

    Inside CreateChildContrls of your derivate you need to create a new instance of Container2 and InstantiateIn. Then get the datasource of SearchResults

    IList<ResultItem> dataSource = SearchManager.Search(
                                    searchQuery,
                                    this.IndexCatalogue,
                                    startIndex,
                                    this.PostsPerPage,
                                    this.WordsMode,
                                    this.EscapeSpecialChars,
                                    out totalItems);

    bind the pager and finally add the container to controls collection.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    13 Sep 2010
    Link to this post
    Hey,
    Beow is my custom class please let me know hoe can I modify these and set the values in each of the private variables like IndexCatalogue, searchQuery, PostsPerPage, etc.

    public class PagerContainer : GenericContainer<SearchResult>
    {
        public PagerContainer(SearchResult owner) : base(owner)
        {
            //
            // TODO: Add constructor logic here
            //
        }
         
        private Pager pager2;
        private Pager pager1;

        public Pager Pager1
        {
            get
            {
                if (this.pager2 == null)
                    pager1 = base.FindRequiredControl<Pager>("pager1");
                return this.pager1;
            }
        }

        public Pager Pager2
        {
            get
            {
                if (this.pager2 == null)
                    this.pager1 = base.FindRequiredControl<Pager>("pager2");
                return this.pager2;
            }
        }

        public string Template
        {
            get
            {
                object o = this.ViewState["Template"];
                if (o == null)
                    return ("~/Sitefinity/ControlTemplates/Search/CustomSearchResult.ascx");
                return (string)o;
            }
            set
            {
                this.ViewState["Template"] = value;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.template = ControlUtils.GetTemplate<PagerTemplate>(this.Template);
            this.template.InstantiateIn(this);
            //IList<ResultItem> datasource = SearchManager.Search(string.Empty, this.IndexCatelogue, 0, this.PostPerPage, 
        }

        public class PagerTemplate : ITemplate
        {
            public void InstantiateIn(Control container)
            {
            }
        }

        private ITemplate template;
    }


    Regards,
    Siddesh Kapadi
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    13 Sep 2010
    Link to this post
    Hello Siddesh Kapadi,

    You do not have to override CreateChildControls of the Container, you have to override CreateChildControls of SearchRestult control and there create an istance of the container and InstantiateIn. Then get the datasource of SearchResults

    protected override void CreateChildControls()
    {
        this.Controls.Clear();
     
        this.PagerContainerCnt = new PagerContainer(this);
        this.LayoutTemplate.InstantiateIn(PagerContainerCnt );
     
    }


    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    14 Sep 2010
    Link to this post
    Hey,
    Should I inherit from SearchResult instead of GenericContainer<SearchResult>.

    Regards,
    Siddesh Kapadi
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    14 Sep 2010
    Link to this post
    Hello Siddesh Kapadi,

    You have to override CreateChildControls of SearchRestult control and there create an instance of the container. You code is valid only for the container and SearchRestult is not of generic type.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    14 Sep 2010
    Link to this post
    Hey,
    below is my code still it doesnt seem to be working.

    public class PagerContainer : GenericContainer<SearchResult>
    {
        public PagerContainer(SearchResult owner) : base(owner)
        {
            //
            // TODO: Add constructor logic here
            //
        }
         
        private Pager pager2;
        private Pager pager1;

        public Pager Pager1
        {
            get
            {
                if (this.pager1 == null)
                    pager1 = base.FindRequiredControl<Pager>("pager1");
                return this.pager1;
            }
        }

        public Pager Pager2
        {
            get
            {
                if (this.pager2 == null)
                    this.pager1 = base.FindRequiredControl<Pager>("pager2");
                return this.pager2;
            }
        }
    }


    public class CustomSearchResult : SearchResult
    {
        public CustomSearchResult()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private PagerContainer pageControl;

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            this.pageControl = new PagerContainer(this);
            this.LayoutTemplate.InstantiateIn(pageControl);
            //base.CreateChildControls();
        }
    }

    Please help me in this

    Regards,
    Siddesh Kapadi
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    14 Sep 2010
    Link to this post
    Hello Siddesh Kapadi,

    I do not see where you are adding the datasource to the pager as it was suggested. In your code it is not possible to get any paging. You have not set the pager datasource nor subscribed for selected index changed of the pager. The current page is not set. I am sending you a sample working implementation.

    template

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="SearchResults.ascx.cs" Inherits="Controls_SearchResults" %>
     
     
    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %>
    <%@ Register Assembly="Telerik.Search" Namespace="Telerik.Search.WebControls.Admin"
        TagPrefix="sfSrc" %>
     
    <sfWeb:CssFileLink ID="CssFileLink1" FileName="~/Sitefinity/ControlTemplates/Search/searchCommonLayout.css" Media="screen" runat="server" />
     
    <sfWeb:Pager ID="pager2" runat="server">
        <LayoutTemplate>
             <asp:Repeater ID="PageRepeaterLinkButton" runat="server">
                <HeaderTemplate>
                    <ol class="sf_pager">
                    <li><asp:LinkButton ID="PreviousPage" runat="server" Text="PreviousPage"></asp:LinkButton></li>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <asp:LinkButton ID="SingleItem" runat="server" />
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    <li><asp:LinkButton ID="NextPage" runat="server" Text="NextPage"></asp:LinkButton></li>
                    </ol>
                </FooterTemplate>
            </asp:Repeater>
            <asp:Repeater ID="PageRepeaterHyperLink" runat="server">
                <HeaderTemplate>
                    <ol class="sf_pager">
                    <li><asp:HyperLink ID="PreviousPage" runat="server" Text="PreviousPage"></asp:HyperLink></li>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <asp:HyperLink ID="SingleItem" runat="server" />
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    <li><asp:HyperLink ID="NextPage" runat="server" Text="NextPage"></asp:HyperLink></li>
                    </ol>
                </FooterTemplate>
            </asp:Repeater>
        </LayoutTemplate>
    </sfWeb:Pager>
     
    <h2><asp:Literal ID="ResultsStats" runat="server">Found {0} pages for "{1}" </asp:Literal></h2>
     
     
    <asp:Repeater ID="rptResults" runat="server">
        <HeaderTemplate>
            <dl class="searchResults">
        </HeaderTemplate>
        
        <ItemTemplate>
            
            <dt><strong><a href='<%#DataBinder.Eval(Container.DataItem, "Url")%>'><%#DataBinder.Eval(Container.DataItem, "Title")%></a></strong></dt> 
            <dd><%#DataBinder.Eval(Container.DataItem, "Snippet")%></dd>
            <dd><em><a href='<%#DataBinder.Eval(Container.DataItem, "Url")%>'><%#DataBinder.Eval(Container.DataItem, "Url")%></a>
                        </em></dd>
        </ItemTemplate>
        <FooterTemplate>
            </dl>
        </FooterTemplate>
    </asp:Repeater>
     
     
    <sfWeb:Pager ID="pager1" runat="server">
        <LayoutTemplate>
             <asp:Repeater ID="PageRepeaterLinkButton" runat="server">
                <HeaderTemplate>
                    <ol class="sf_pager">
                    <li><asp:LinkButton ID="PreviousPage" runat="server" Text="PreviousPage"></asp:LinkButton></li>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <asp:LinkButton ID="SingleItem" runat="server" />
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    <li><asp:LinkButton ID="NextPage" runat="server" Text="NextPage"></asp:LinkButton></li>
                    </ol>
                </FooterTemplate>
            </asp:Repeater>
            <asp:Repeater ID="PageRepeaterHyperLink" runat="server">
                <HeaderTemplate>
                    <ol class="sf_pager">
                    <li><asp:HyperLink ID="PreviousPage" runat="server" Text="PreviousPage"></asp:HyperLink></li>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <asp:HyperLink ID="SingleItem" runat="server" />
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    <li><asp:HyperLink ID="NextPage" runat="server" Text="NextPage"></asp:HyperLink></li>
                    </ol>
                </FooterTemplate>
            </asp:Repeater>
        </LayoutTemplate>
    </sfWeb:Pager>

    custom search results control

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Search.WebControls;
    using System.Web.UI;
    using System.IO;
    using System.Web.UI.WebControls;
    using Telerik.Cms.Web.UI;
    using Telerik.Search.Common;
    using Telerik.Search.Engine;
    using Telerik.Utilities;
    using System.Globalization;
    using Telerik.Cms.Web;
    using System.ComponentModel;
    using Telerik.Framework.Web;
    using Telerik.Localization;
    using System.Collections;
     
    /// <summary>
    /// Summary description for SearchResultsCustom
    /// </summary>
    ///
    namespace Sitefinity.Samples
    {
     
        public class ResultToolboxItemCustom : System.Drawing.Design.ToolboxItem
        {
            public ResultToolboxItemCustom()
                : base(typeof(SearchResultsCustom))
            {
                base.DisplayName = "CustomSearch";
                base.Description = "Custom Search Control";
            }
        }
     
        [System.ComponentModel.ToolboxItem(typeof(ResultToolboxItemCustom))]
        public class SearchResultsCustom : SearchResult
        {
     
            /// <summary>
            /// Gets or sets the template used by SearchResultsCustom control
            /// </summary>
            [Browsable(false)]
            [DefaultValue((string)null)]
            [TemplateContainer(typeof(SearchResult), BindingDirection.OneWay)]
            [PersistenceMode(PersistenceMode.InnerProperty)]
            public ITemplate LayoutTemplate
            {
                get
                {
                    if (this.layoutControlTemplate == null)
                        this.layoutControlTemplate = ControlUtils.GetTemplate<DefaultTemplate>(this.CustomControlLayoutTemplatePath);
                    return this.layoutControlTemplate;
                }
                set
                {
                    this.layoutControlTemplate = value;
                }
            }
     
     
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
                this.pageCnt = new PageContainer(this);
                this.LayoutTemplate.InstantiateIn(pageCnt);
     
              if (this.AllowPaging)
              {
     
                    if (!string.IsNullOrEmpty(Query))
                    {
                        string vPath = PathUtil.GetIndexPhysicalPath(this.IndexCatalogue);
                        if (Directory.Exists(vPath))
                        {
                            if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[PageKey]))
                            {
                                this.CurrentPage = Convert.ToInt32(
                                    HttpContext.Current.Request.QueryString[PageKey]);
                            }
                            string searchQuery = Query;
                            int totalItems;
                            int startIndex = (this.CurrentPage - 1) * this.PostsPerPage;
                            IList<ResultItem> dataSource1 = SearchManager.Search(
                                          searchQuery,
                                          this.IndexCatalogue,
                                          startIndex,
                                          this.PostsPerPage,
                                          this.WordsMode,
                                          this.EscapeSpecialChars,
                                          out totalItems);
                            int numberOfPages = (this.PostsPerPage == 0) ? 1 : (int)Math.Ceiling((double)totalItems / (double)this.PostsPerPage);
                            this.pageCnt.Pager2.SelectedPageChanged += new EventHandler<EventArgs>(Pager2_SelectedPageChanged);
                            this.pageCnt.Pager2.PageCount = numberOfPages;
                            this.pageCnt.Pager2.SelectedPage = this.CurrentPage;
                           
                        }
                    }
                }
              Controls.Add(this.pageCnt);
            }
     
            void Pager2_SelectedPageChanged(object sender, EventArgs e)
            {
                this.CurrentPage = ((Pager)sender).SelectedPage;
                this.ChildControlsCreated = false;
            }
     
            protected ITemplate layoutControlTemplate;
            private string CustomControlLayoutTemplatePath = "~/Controls/SearchResults.ascx";
     
            protected class PageContainer : GenericContainer<SearchResultsCustom>
            {
                public PageContainer(SearchResultsCustom owner)
                    : base(owner)
                {
                }
     
                public Pager Pager2
                {
                    get
                    {
                        if (this.pager2 == null)
                            this.pager2 = base.FindRequiredControl<Pager>("pager2");
                        return this.pager2;
                    }
                }
                private Pager pager2;
            }
     
            protected PageContainer pageCnt;
        }
    }


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  11. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    15 Sep 2010
    Link to this post
    Hey Ivan,
    Thanx for the working prototype. Just a small erroe that I get.

    A required control was not found in the template for "~/Sitefinity/ControlTemplates/Search/SearchResult.ascx". The control must be assignable form type "Telerik.Cms.Web.UI.Pager" and must have ID "pager2".

    I find the above error at line:
    this.pager2 = base.FindRequiredControl<Pager>("pager2");

    on property Pager2

    My page has this control with the said ID. Still it is unable to find it :-(

    Please help.

    Regards,
    Siddesh Kapadi
  12. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    16 Sep 2010
    Link to this post
    Hey,
    I got this to work.
    I followed the same approach as you, but with just a little tweak.
    I need to create another file which as the pager2 defined.

    ONLY pager2 needs to be available in the file. No repeater control neither pager1.

    Specify the path of the file containing the pager2 control in line:

    private string CustomControlLayoutTemplatePath ="~/Controls/SearchResults.ascx";

    Also, DO NOT add the control at the bottom of the page replace the line:

    Controls.Add(this.pageCnt);
    With:
    Controls.AddAt(0, this.pageCnt);

    This would add the control at the top of the usercontrol.

    This wouold certainly help the needy..

    Regards,
    Siddesh Kapadi
Register for webinar
12 posts, 1 answered