Click here to Skip to main content
15,896,481 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi every one forum

i am working on sql server 2008 and visual studio 2010 asp.net c#

i have a question how make a searchable dropdownlist
and if i type letter A it should bring all items that begin with letter A ...like autosuggest

any help will be appreciated

What I have tried:

i have searched google not found what i looking for
Posted
Updated 7-Jul-17 5:57am

 
Share this answer
 
Comments
haiderjaafer 7-Jul-17 16:20pm    
thank for your response Ramesh Kumar Barik

its helpful but when i type a letter A it should only bring all items that begin with letter A... now its bring items contain a letter A any solution for this
Ramesh Kumar Barik 8-Jul-17 9:27am    
Yes, It because of the query you have written in sql to retrieve the list.
If you want the result should come on the first letter then use % after the input parameter in like operator.

Ex: select * from dbo.TableName where ColumnName like 'a%'.
haiderjaafer 13-Jul-17 16:58pm    
hi Ramesh Kumar Barik thank

i have not use of parameters in this case ...
i have set dropdownlist to select database field
SELECT * FROM Org_Table
If you are not interested in going for JQuery auto complete or have specific need, you have the option of using a text box, which will be used to type the text to search and add a dropdownlist or list control in update panel which will be refreshed while you are typing text in textbox.

this is the code you need in your .aspx

<script type="text/javascript">
    function RefreshUpdatePanel() {
        __doPostBack('txtSearch', '');
    };
</script>


<asp:TextBox ID="txtSearch" runat="server" onkeyup="RefreshUpdatePanel();" AutoPostBack="false" OnTextChanged="txtSearch_TextChanged" ></asp:TextBox>
    <asp:UpdatePanel ID="Update" runat="server">
        <ContentTemplate>
            <asp:DropDownList runat="server" ID="ddlSearch" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="txtSearch" />
        </Triggers>
    </asp:UpdatePanel>

    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>


Below is the code needed in your code behind.

public List<string> Values { get; set; }
        private void GetValues()
        {

            Values = new List<string>();

            Values.Add("Apple");
            Values.Add("Orange");
            Values.Add("Banana");
            Values.Add("Pear");
            Values.Add("Black Berry");
            Values.Add("Pineapple");
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var dropDownList = sender as TextBox;
            var options = (from o in Values
                           where o.StartsWith(dropDownList.Text, StringComparison.InvariantCultureIgnoreCase)
                           select o).ToList();

            ddlSearch.DataSource = options;
            ddlSearch.DataBind();
        }


This approach updates the panel contents onkeyup event of the textbox, if you need this on other event e.g. onblur, you can register it from code behind using "RegisterClientScriptBlock".

I would still recommend you to go for JQuery auto complete because it is more cleaner approach.
 
Share this answer
 
check this plugin -select2[^]
 
Share this answer
 

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