Ajax autocomplete extender and get ID of selected item?

13,672

Solution 1

The AutoCompleteExtender merely extends the ASP.NET TextBox control, so if you want to know when the text changed, then just raise the TextChanged event on the TextBox control, like this:

Markup:
<asp:TextBox ID="TextBox1" runat="server" Height="27px" Width="570px" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>

Code-Behind:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    // Do something here with textbox value
}

Solution 2

For this you need to return the list from web service method with ID and Text

Here "lst" is the actual list with data from your data source.

List<string> items = new List<string>(count);
        for (int i = 0; i < lst.Count; i++)
        {
            string str =AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(lst[i].Text,Convert.ToString(lst[i].IDValue));                
            items.Add(str);

        }
        return items.ToArray();

Then simple javascript

function GetID(source, eventArgs )
    {
        var HdnKey = eventArgs.get_value();
        document.getElementById('<%=hdnID.ClientID %>').value = HdnKey;
    }

and dont forget to set the attribute in auto complete extender OnClientItemSelected="GetID"

Share:
13,672
user1342164
Author by

user1342164

Updated on June 12, 2022

Comments

  • user1342164
    user1342164 almost 2 years

    I am using the autocomplete extender to suggest names when a user types. How to get the select value after the user selects an item? I guess I can use onclientitemselected but I am not familiar on how to write this? I need to populate a textbox based on the selection in the autocompleteextender textbox. Thank you

     <asp:TextBox ID="TextBox1" runat="server" Height="27px" Width="570px"></asp:TextBox>
            <asp:AutoCompleteExtender ID="AutoCompleteExtender" runat="server" 
        DelimiterCharacters="" Enabled="True" ServicePath="AutoComplete.asmx"  
        ServiceMethod="GetCompletionList" TargetControlID="TextBox1" 
        MinimumPrefixLength="2" UseContextKey="true" ContextKey="StateDropDown" 
                CompletionListElementID="autocompleteDropDownPanel">
    </asp:AutoCompleteExtender>