Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datalist above which i have a textbox.
and what i want to do is as i enter a character in textbox, only items corresponding to that char should b filled in datalist.and all process should be on onkeypress event of textbox

NOTE: there is only 900 names list in datalist.
Posted
Comments
Muralikrishna8811 10-Oct-11 7:02am    
you want to sort that data in c# or Jquery

You have to use autopostback, or (faster) ajax like this:

XML
<asp:TextBox runat="server" ID="txtFilter" autocomplete="off" ></asp:TextBox>
<asp:LinkButton runat="server" ID="lbFilter" onclick="FilterDropdown" Text="filter" style="visibility:hidden;"></asp:LinkButton>
<asp:ScriptManager runat="server" ID="scrMang"></asp:ScriptManager>
<asp:UpdatePanel ID="updSelction" runat="server">
    <ContentTemplate>
        <asp:ListBox runat="server" ID="ddl" ></asp:ListBox>
    </ContentTemplate>
    <Triggers >
        <asp:AsyncPostBackTrigger ControlID="lbFilter" />
    </Triggers>
</asp:UpdatePanel>



C#
public partial class _Default : System.Web.UI.Page
{
    string[] Names = {"Andreas",
                     "Benjamin",
                     "Caroline",
                     "Daniel",
                     "Emil",
                     "Franziska",
                     "Gustav",
                     "Hans"
                     };
    protected void Page_Load(object sender, EventArgs e)
    {
        txtFilter.Attributes.Add("onkeyup", "__doPostBack('" + lbFilter.UniqueID + "','');");
        if(!IsPostBack) {
            foreach(var name in Names) {
                ddl.Items.Add(name);
            }
        }
    }

    protected void FilterDropdown(object sender, EventArgs e) {
        ddl.Items.Clear();
        foreach(var name in Names) {
            if(name.ToLower().Contains(txtFilter.Text.ToLower())) {
                ddl.Items.Add(name);
            }
        }
        txtFilter.Focus();
        if(ddl.Items.Count >0)
        ddl.Items[0].Selected = true;
    }
}
 
Share this answer
 
If you know how to use LINQ then you can write only a couple of lines of code and achieve the desired outcome:

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string search = textBox1.Text;
            listBox1.DataSource = (from l in list where l.Contains(search) select l).ToList();
        }



"list" is just a simple list of strings defined in the constructor and populated. Try it and hack around with it.

Cheers...

EDIT: here is a good start for LINQ if you don't already know it, http://msdn.microsoft.com/en-us/netframework/aa904594[^]

EDIT: use
C#
l.StartsWith(search)
instead of
C#
l.Contains(search)
which does a wild search. StartsWith searches for the beginning of the string which is more accurate.
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900