More in this section

Forums / Developing with Sitefinity / custom search functionality help

custom search functionality help

2 posts, 0 answered
  1. Meister
    Meister avatar
    262 posts
    09 Nov 2007
    19 Sep 2008
    Link to this post


    I have an animal site with the following page structure

    -----cat 1
    -----cat 2
    -----golden lab
    -----Boxer dog

    And I have 2 search indexes on the site which searches within all the animal pages:
    1. Cats Search
    2. Dogs Search

    BUT, i cannot have a free text search box!

    What i need to have is a custom control with the following functionality:
    The page will contain 2 drop downs
    1. animals dropdown (only 2 items, cat and dog)
    2. a list of hardcoded key words in a dropdown

    When the user clicks the search button, it will look to the first drop down to see which search index to use (i.e. dogs) then it will look to the second drop down and use the selected value as the keyword for the search.

    theres a bit more to it, but this is the bit i need clarification on

    How would i go about implemeting something like this? any help appreciated
    hope i explained this well enough?



  2. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    09 Sep 2007
    19 Sep 2008
    Link to this post

    Hi Quade,

    When working with a normal Sitefinity search box, click search and look at the URL.  You'll see something like this:


    All we need to do is pass those 2 query string parameters (IndexCatalogue & SearchQuery) to the results page and we're good to go.  We could do this via a simple "Response.Redirect". 

    Here is a sample UserControl that does this:

    Create ~/UserControls/CustomSearchBox.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomSearchBox.ascx.cs" Inherits="UserControls_CustomSearchBox" %> 
    <asp:Label AssociatedControlID="PetType" runat="server">Search </asp:Label> 
    <asp:DropDownList ID="PetType" runat="server">  
        <asp:ListItem Value="Dogs" /> 
        <asp:ListItem Value="Cats" /> 
    <asp:Label AssociatedControlID="Keyword" runat="server"> for </asp:Label> 
    <asp:DropDownList ID="Keyword" runat="server">  
        <asp:ListItem Value="World's Smartest Dogs - aka Border Collies" /> 
        <asp:ListItem Value="Yappy Ankle Biters" /> 
        <asp:ListItem Value="Prissy Aloof Cats - returns all cats" /> 
    <asp:Button ID="SearchButton" Text="Search" OnClick="SearchButton_Clicked" runat="server" /> 


    Create ~/UserControls/CustomSearchBox.ascx.cs

    using System;  
    using System.Collections;  
    using System.Configuration;  
    using System.Data;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    public partial class UserControls_CustomSearchBox : System.Web.UI.UserControl  
        protected void Page_Load(object sender, EventArgs e)  
        protected void SearchButton_Clicked(object sender, EventArgs e)  
            // When search button is clicked, redirect to "results" page with querystring parameters.  
            string ResultsUrl = "~/Results.aspx";  
            ResultsUrl += "?IndexCatalogue=" + HttpUtility.UrlEncode(PetType.SelectedValue);  
            ResultsUrl += "&SearchQuery=" + HttpUtility.UrlEncode(Keyword.SelectedValue);  
            // Redirect to search results URL.  


    Alter your web.config file and add the following line to the <toolboxControls> section:

    <add name="Custom Search Box" section="Search" url="~/UserControls/CustomSearchBox.ascx" /> 

    So there we have it; we've clearly established Border Collies are superior to all other pets.

    :) :) :)

    Let me know if you have problems.  Also, as a dog lover and avid dog trainer, I would love to get a look at your web site when it's ready.  Feel free to toss me an email to


    Gabe Sumner


2 posts, 0 answered