Click here to Skip to main content
15,889,281 members
Articles / .NET
Tip/Trick

Predictive Search Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Jan 2011CPOL1 min read 16.1K   4   2
Contains the custom control (Predictive Search Control) developed in .NET Framework 2.0

Introduction


Predictive Search Control is a custom control that works similar to Google Current Search. As we all know, Search is one of the most important functionalities in any application. Here Search is Predictive since the search results are displayed as and when a charcter is being typed. The results are displayed that matches with the charaters entered in the search. This uses extensively JavaScript to perform the search.


Create Predictive Search Control


Add a Class Library Project and also add a class that inherits from CompositeControl Composite Control consists of more than one ASP.NET Server control that enables the user to extend functionlity. For more information on comp:


   public class PredictiveSearchControl :CompositeControl
   {
        private TextBox txtSearch = new TextBox();
        private ListBox lstBox = new ListBox();

   public object> DataSource
   {
         get
        {
            return lstBox.DataSource;
            }
        set
       {
            lstBox.DataSource = value;
        }
    }

   public bool IgnoreCaseSensitive{ get;  set; }

   public string DisplayMember
   {
        get
       {
            return lstBox.DataTextField;
        }
        set
       {
            lstBox.DataTextField = value;
        }
    }

   public string ValueMember
   {
        get
       {
            return lstBox.DataValueField;
        }
        set
       {
            lstBox.DataValueField = value;
        }
    }

    public override  Unit  Width{ get; set ; }

    public override void RenderControl(HtmlTextWriter writer)
   {
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        txtSearch.RenderControl(writer);
        writer.RenderEndTag();    // td
        writer.RenderEndTag();    // tr
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        lstBox.RenderControl(writer);
        writer.RenderEndTag();    // td
        writer.RenderEndTag();    // tr
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.RenderEndTag();    // td
        writer.RenderEndTag();    // tr
        writer.RenderEndTag();    // table
    }

   public override ControlCollection Controls
   {
        get
       {
            EnsureChildControls();
            return base.Controls;
        }
    }

    protected override void CreateChildControls()
   {
        Controls.Clear();
        txtSearch.Width = this.Width;
        Controls.Add(txtSearch);
        lstBox.Width = this.Width;
        lstBox.DataBind();
        Controls.Add(lstBox);
        //The description for these Javascript functions
        //have been explained in the
        lstBox.Attributes.Add("onchange", "Javascript:setTextOnChange()");
        lstBox.Attributes.Add("ondblclick",
            "Javascript:SetVisibility('false')");
        lstBox.Attributes.Add("onclick", "Javascript:SetVisibility('false')");
        txtSearch.Focus();
        txtSearch.Attributes.Add("onkeyup",
            "Javascript:Test('" + lstBox.ClientID + "','" +
            txtSearch.ClientID + "','" + this.IgnoreCaseSensitive + "') ");
        txtSearch.Attributes.Add("onfocus", "Javascript:Focus('" +
            lstBox.ClientID + "')");
    }

    protected override void OnPreRender(EventArgs e)
   {
        base.OnPreRender(e);
        this.Page.ClientScript.RegisterClientScriptInclude(
            "PredictiveSearch", this.Page.ClientScript.GetWebResourceUrl(
            typeof(PredictiveSearchControl),
            "PredictiveSearchControl.PredictiveSearch.js"));
    }
}

Test Web Page: Using the Control


Create a Test Web Site that uses the control created above. The following is the HTMLCode for the control. As can be seen below, ignorecasesensitive can be configured. Setting it to TRUE will display all the entries matching the text entered ignoring the case. Setting it to FALSE will display only those entries matching the text considering the case.


<cc:predictivesearchcontrol runat"=server"
width"=170" displaymember"=EmpLastName"
valuemember"=EmpId"  id "=testPredicateSearch"
ignorecasesensitive="=false"/>

In the Page_Load() event of the test page, assign the datasource to the control


Pros and Cons


Pros



  • Very fast search
  • Can be used in application that has the static data for long time
  • Very easy to use for the developer
  • Search can be configured as per the user requirement

Cons



  • Cannot be used in applications where the data varies frequently

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioni can't see how it work Pin
Ahmed Gamal Eldeeb10-May-14 3:48
Ahmed Gamal Eldeeb10-May-14 3:48 
QuestionDatasource & getting value in code behond Pin
Jez129-May-12 4:41
Jez129-May-12 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.