ajaxToolkit AutoCompleteExtender click 选择某项之后触发事件
What I wanted to do is using the autocomplete, search a receiver table, once a user selected a particular item in the autocomplete, the ReceiverID of the particular item is then somehow passed through to the code behind, and this ReceiverID is then used to display the receiver details in a panel below the autocomplete.
This was tricking as it involved both client side Javascript functions and code behind, but this was how I ended up doing it. First setup a normal ajax toolkit autocomplete, there is plenty of documentation out in the web world for this so I won’t go through in detail. In the webservice function where the data is retieved from the database, and added to a string array, the following will need to be used
Dim dr As System.Data.SqlClient.SqlDataReader
dr = dsReceverMgt.selectAllReceiverMnagement(prefixText)While dr.Read
sQuickName = dr(”ReceiverQuickName”)
sCompany = dr(”ReceiverCompany”)
sLocationName = dr(”ReceiverAddressTown”)
Co = sQuickName & “,” & sCompany & “,” & dr(”ReceiverAddress1″) & “,” & sLocationName
If sQuickName.StartsWith(prefixText, StringComparison.OrdinalIgnoreCase) Or sCompany.StartsWith(prefixText, StringComparison.OrdinalIgnoreCase) Or sLocationName.StartsWith(prefixText, StringComparison.OrdinalIgnoreCase) Then
‘—add the member name to the list if the text starts with the variables—
listOfMembersStartsWith.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(Co, dr(”ReceiverPK”)))
Else
‘—add the member name to the list if the text contains the keyword but not as an prefix—
listOfMembersNotStartsWith.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(Co, dr(”ReceiverPK”)))
End If
End While
using this function AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem, the items is inserted with a value/text combination.
In the autocomplete declaration it self on the aspx code, this attribute need to be placed on the autocomplete OnClientItemSelected=”SetSelectedValue” , what this will trigger is on an selection in autocomplete, it will call a javascript function called SetSelectedValue, and here is the javascript declaration
function SetSelectedValue( source, eventArgs ) {
document.getElementById((’ctl00_leftContent_SelectedReceiver’)).value=eventArgs.get_value();
}or, fire a button’s click event:
function SetSelectedValue( source, eventArgs ) {
var btnSearch=document.getElementById("ctl00_ContentPlaceHolder1_btnSearch");
if (btnSearch)
{btnSearch.click(); }
}
What I have created on the page is a hidden input, so on fireing of the event by the autocomplete when an item is selected, it will set the hidden inputs value with the eventArgs.get_value(), which in my case is the primary key for the Receiver row
<input type=”hidden” id=”SelectedReceiver” runat=”server” />
once this is done, the selected pk is available.
The receiver details is displayed once a person clicks on a Go button, so in my Go button’s onclick event function, I have the following code, which then goes and retrievs the receiver details
Protected Sub btnGoReceiver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGoReceiver.Click
DisplayReceiverDetails(SelectedReceiver.Value)
End Sub
This can be easily modfied to retrieve the details on an autocomplete selection by adding a line to the SetSelectedValue function, which clicks the go button via javascript to trigger the action
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2008-07-16 校级选修课《软件开发实践》教学大纲(200807修订)