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

Forums / Developing with Sitefinity / Custom Search Results Not Working

Custom Search Results Not Working

2 posts, 0 answered
  1. mcpdinkansas
    mcpdinkansas avatar
    102 posts
    Registered:
    22 Mar 2007
    19 Apr 2010
    Link to this post
    I created a custom Search Results control, yet it seems to not work. It displays in the Controls, and can be added to a page, but it seems to point to the original SearchResult.ascx file, and not my new UserControl ASCX. It does not throw any errors, but points to the default SearchResult.ascx file.

    Here is my code:

    My ASCX UserControl
    PageGroupSearchResultsControl.ascx

    <%@ Control Language="C#" %>
    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %>
    <%@ Register Assembly="Smart401kSitefinityLib" Namespace="Telerik.Search.WebControls.Admin" TagPrefix="sfSrc" %>
     
    <sfWeb:CssFileLink ID="CssFileLink1" FileName="~/Sitefinity/ControlTemplates/Search/searchCommonLayout.css" Media="screen" runat="server" />
     
    <asp:Label ID="LabelTest" runat="server"></asp:Label>
     
    <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="Previous Page"></asp:LinkButton></li>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <asp:LinkButton ID="SingleItem" runat="server" />
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    <li><asp:LinkButton ID="NextPage" runat="server" Text="Next Page"></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="Previous Page"></asp:HyperLink></li>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <asp:HyperLink ID="SingleItem" runat="server" />
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    <li><asp:HyperLink ID="NextPage" runat="server" Text="Next Page"></asp:HyperLink></li>
                    </ol>
                </FooterTemplate>
            </asp:Repeater>
        </LayoutTemplate>
    </sfWeb:Pager>

    My Custom Class
    PageGroupSearchResultsControl.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
     
    namespace Lib.ui.WebControls
    {
        using Telerik.Framework.Web.Design;
        using Telerik.Cms.Web.UI;
        using Telerik.Search.WebControls;
        using Telerik.Search.Common;
        using System.IO;
        using System.Collections.Generic;
        using Telerik.Search.Engine;
        using Telerik.Utilities;
        using Telerik.Web;
     
        public class PageGroupSearchResultsControl : SearchResult
        {
            private Container container;
     
            public PageGroupSearchResultsControl()
            {
                 
            }       
     
            protected override void CreateChildControls()
            {
                this.Controls.Clear();
     
                this.LayoutTemplatePath = "~/Sitefinity/UserControls/CustomSearchResults/PageGroupSearchResultsControl.ascx";
                this.layoutTemplate.InstantiateIn(container);
     
                string CatalogName = "Full+Site+Page+Index";
     
                base.CreateChildControls();
     
                this.container = new Container(this);
     
                this.container.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.container.ResultsList.DataBind();           
     
                if (!string.IsNullOrEmpty(Query))
                {
                    string vPath = PathUtil.GetIndexPhysicalPath(this.IndexCatalogue);
                    string nPath = PathUtil.GetIndexPhysicalPath(CatalogName);
                    if (Directory.Exists(vPath) && Directory.Exists(nPath))
                    {
                        // check if query string is supplied to select the current page
                        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[PageKey]))
                        {
                            this.CurrentPage = Convert.ToInt32(
                                HttpContext.Current.Request.QueryString[PageKey]);
                        }                 
     
                        string searchQuery = "Test";
                        string message = string.Empty;
     
                        bool isValid = this.EscapeSpecialChars ? this.EscapeSpecialChars : ValidateQuery(ref searchQuery, out message);
     
                        if (isValid)
                        {
                            int totalItems = 0;
                            int totalItems2 = 0;
                            int startIndex = (this.CurrentPage - 1) * this.PostsPerPage;
     
                            try
                            {
     
                                IList<ResultItem> DataSource = SearchManager.Search(
                                       searchQuery,
                                       CatalogName,
                                       startIndex,
                                       this.PostsPerPage,
                                       this.WordsMode,
                                       this.EscapeSpecialChars,
                                       out totalItems2);
     
                                foreach (ResultItem customItem in DataSource)
                                {
                                    if (customItem.Url.Contains(""))
                                    {
                                        string test = "";
                                    }
                                }
     
                                totalItems = totalItems + totalItems2;
     
                                int numberOfPages = (this.PostsPerPage == 0) ? 1 : (int)Math.Ceiling((double)totalItems / (double)this.PostsPerPage);
     
                                if (numberOfPages == 0 && totalItems > 0)
                                    numberOfPages = 1;
     
                                string qeryTest = this.Query.Trim('\"');
                                ((PageGroupSearchResultsControl)this.layoutCnt.ResultsStats).EnableViewState = false;
                                this.container.ResultsStats.Text = string.Format(this.container.ResultsStats.Text, totalItems, qeryTest);
     
                                if (this.AllowPaging)
                                {
                                    this.container.Pager1.SelectedPageChanged += new EventHandler<EventArgs>(Pager_SelectedPageChanged);
                                    this.container.Pager1.PageCount = numberOfPages;
                                    this.container.Pager1.SelectedPage = this.CurrentPage;
                                }
                                this.container.ResultsList.DataSource = DataSource;
     
                            }
                            catch (Telerik.Lucene.Net.QueryParsers.ParseException ex)
                            {
                                this.container.ResultsStats.Text = "strange chars";
                                Log.Exception(ex);
                            }
                        }
                        else
                        {
                            this.container.ResultsStats.Text = message;
                        }
                    }
                    else
                    {
                        this.container.ResultsStats.Text = String.Format("InvalidIndex", this.IndexCatalogue);
                    }
                }
                else
                {
                    this.container.ResultsStats.Text = String.Empty;
                }
     
                this.container.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
                this.container.ResultsList.SkinID = this.SkinID;
     
                this.Controls.Add(this.container);
                this.container.ResultsList.DataBind();
     
            }
     
            void ResultsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    if (!this.AllowPaging)
                    {
                        e.Item.Controls.Clear();
                        return;
                    }
                }
            }
     
            void Pager_SelectedPageChanged(object sender, EventArgs e)
            {
                this.CurrentPage = ((Pager)sender).SelectedPage;
                this.ChildControlsCreated = false;
            }
        }
    }

    My Web.config entry

    <add name="Page Group Search Results" section="Search" type="Lib.ui.WebControls.PageGroupSearchResultsControl, Lib"
              description="Displays search results for individual Page Groups" />


    Any help would be greatly appreciated!

    Thanks
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    19 Apr 2010
    Link to this post
    Hello mcpdinkansas,

    You need to create a toolbox item for you serchbox control.

    public class SearchBoxToolboxItemCustom : ToolboxItem
        {
             
            public SearchBoxToolboxItemCustom ()
                : base(typeof(PageGroupSearchResultsControl))
            {
                base.DisplayName = "";
                base.Description = "";
     
            }
        }


    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.
2 posts, 0 answered