Ajax autocomplete extender and get id of selected item?

I use an autocomplete extender to suggest names as I type. How to get selection value after user select an item? I think I can use onclientitemselected, but I do not know how to write this? I need to fill in a text box based on the selection in the autocompleteextender text box. 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> 
+5
source share
3 answers

AutoCompleteExtender simply extends the ASP.NET TextBox control, so if you want to know when the text changed, just raise the TextChanged event in the TextBox , for example:

 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 } 
0
source

To do this, you need to return the list from the web service method with identifier 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 don't forget to set the attribute in autocomplete OnClientItemSelected = "GetID"

+4
source

what does hdnID mean hidden value? if it is a hidden value, what value will it be with

0
source

Source: https://habr.com/ru/post/1488555/


All Articles