Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box where you enter some name . i have a database which contains list of the names available . now if someone enters 'a' in the text box it should dropdown all names starting from 'a'.If someone enters 'b' now whole text will become 'ab' so it should dropdown all names starting with 'ab' .any pointers how to implement this functionality ?
Posted

Hello dear,

It is very simple to achieve this functionality. When user enters any name in the textbox then in the textbox text changed event fire a query to the database using like operator which will give you all the names starting with the letter entered by the user. Then bind the data to the dropdownlist. The query will be

select * from <your table> where name like " + "'" + textbox.text + "%" + "'"
 
Share this answer
 
You need to use a ComboBox instead of a TextBox. Further, you don't want to necessarily change the contents of the combobox. You merely want to change the selection based on the currently typed text. Changing the binding every time a character is typed is going to cause too much overhead.
 
Share this answer
 
This Functionality is called as AutoComplete TextBox
in asp.net 3.5 it supports AjaxControlToolKit which is open source & can be used in ur application.
First Download AjaxControlToolKit from Following Location.

http://ajaxcontroltoolkit.codeplex.com/[^]

Then in ur asp.net bin Folder Copy the dll of AjaxControlToolKit.

& add Following Line of code at top of ur asp.net page.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


Add ScriptManager to page, Which required for Ajax Application.

XML
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>


Add One WebService file(.asmx) into ur project.

after adding .asmx file .cs file will saved in App_code Folder. add this type of method in .cs file.

C#
[WebMethod(EnableSession = true)]
    public string[] ItemName(string prefixText,int count)
    {
        int branchId = (int)Session["BranchId"];
        using (var db = new WanaDCDataContext())
        {
            var items = new List<String>();
            JavaScriptSerializer serailizer = new JavaScriptSerializer();
            var list = (from it in db.ItemMasters where it.BranchId == branchId && (it.ItemName).ToLower().StartsWith(prefixText) orderby it.ItemName ascending select it).Take(count);
            foreach (ItemMaster c in list)
            {
                var html = c.ItemId;
                //var item = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(c.ItemName, c.ItemId);
                string item = AutoCompleteExtender.CreateAutoCompleteItem(c.ItemName, serailizer.Serialize(html));
                items.Add(item);
            }
            return items.ToArray();


        }
    }


This is Web Service Methods Which Returns String array. i have used linq To SQL Classes to retrieve data from Database.


Then call this web service from asp.net page as below.

XML
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" TargetControlID="txtStkItem"
                                        OnClientItemSelected="OnItemSelected" CompletionSetCount="60" ServiceMethod="ItemName"
                                        ServicePath="AjaxAutoCompleteService.asmx" MinimumPrefixLength="1" CompletionInterval="100"
                                        CompletionListCssClass="completionList" CompletionListItemCssClass="listItem"
                                        CompletionListHighlightedItemCssClass="itemHighlighted" runat="server">
                                    </cc1:AutoCompleteExtender>


Make Sure You specify correct service Method,Service Path. reply for any difficulty.

Thanks & Regards,
satya.
 
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