单击某个项目时如何让 AutoCompleteExtender 提交

发布于 2024-10-21 05:03:31 字数 153 浏览 3 评论 0原文

我的 ASP.net 表单上有一个文本框和一个按钮,用于执行搜索。我从 AJAX 工具包添加了一个 autocompleteextender,以便在用户键入时显示建议。这工作正常,但是我想要发生的是当用户在显示的建议列表中选择一个项目时触发按钮的 Click 事件。有人知道如何做到这一点吗?

I have a textbox and a button on my ASP.net form for executing a search. I have added an autocompleteextender from the AJAX toolkit to show suggestions while the user is typing. This works fine, however what I want to happen is for the Click event of the button to fire when the user selects an item in the displayed list of suggestions. Anyone any idea how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的温暖 2024-10-28 05:03:31

由于项目选择事件将触发客户端 JavaScript 事件,因此我通常将以下代码添加到 OnClientItemSelected 事件方法中:

<script type="text/javascript" language="javascript">
   function YourMethodHere(source, eventArgs)
   {
      $get('ctl00_BodyPlaceHolder_btnAutoSubmit').click();
   }
</script>

您需要相应地找到按钮的正确名称并将其替换到上面。

作为一项附加功能,有时我希望能够在“自动完成”中输入一个值,如果我知道我想要什么,就立即按回车键。为了实现这一点,您需要将自动完成文本框和按钮包装在面板中,并相应地设置默认按钮:

<asp:Panel ID="pnlAutoCompleteStuff" runat="server" DefaultButton="btnAutoSubmit">
   Search: <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
           <cc1:AutoCompleteExtender ID="aceSearch" runat="server"
                                      TargetControlID="txtSearch"                             
                                      ServiceMethod="YourMethodHere"                   
                                      ServicePath="YourServicePath"                                   
                                      MinimumPrefixLength="4"
                                      CompletionInterval="500"
                                      EnableCaching="False"
                                      OnClientItemSelected="AutoCompleteClientMethod"
                                      CompletionSetCount="3">
            </cc1:AutoCompleteExtender>
   <asp:Button ID="btnAutoSubmit" runat="server" Text="Select" />
</asp:Panel

Since the item selected event is going to fire a client side JavaScript event, I typically add the following code to my OnClientItemSelected event method:

<script type="text/javascript" language="javascript">
   function YourMethodHere(source, eventArgs)
   {
      $get('ctl00_BodyPlaceHolder_btnAutoSubmit').click();
   }
</script>

You'll need to find the proper name of your button accordingly and replace it above.

As an added feature, sometimes I want to be able to type a value in to the AutoComplete and hit the enter key right away if I know what I want. To accomplish this, you'll want to wrap your AutoComplete textbox and button in a panel and set the default button accordingly:

<asp:Panel ID="pnlAutoCompleteStuff" runat="server" DefaultButton="btnAutoSubmit">
   Search: <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
           <cc1:AutoCompleteExtender ID="aceSearch" runat="server"
                                      TargetControlID="txtSearch"                             
                                      ServiceMethod="YourMethodHere"                   
                                      ServicePath="YourServicePath"                                   
                                      MinimumPrefixLength="4"
                                      CompletionInterval="500"
                                      EnableCaching="False"
                                      OnClientItemSelected="AutoCompleteClientMethod"
                                      CompletionSetCount="3">
            </cc1:AutoCompleteExtender>
   <asp:Button ID="btnAutoSubmit" runat="server" Text="Select" />
</asp:Panel
独守阴晴ぅ圆缺 2024-10-28 05:03:31

将文本框的 Autopostback 设置为 true。

<asp:TextBox ID="SearchCityBox" CssClass="searchOne" runat="server" Width="500px" Height="18px" AutoPostBack="true" OnTextChanged="SearchCityBox_TextChanged"></asp:TextBox>

然后创建一个 onTextChanged 事件处理程序来执行与按钮相同的操作。或者您可以将其指向按钮所具有的相同事件处理程序

Set the textbox's Autopostback to true.

<asp:TextBox ID="SearchCityBox" CssClass="searchOne" runat="server" Width="500px" Height="18px" AutoPostBack="true" OnTextChanged="SearchCityBox_TextChanged"></asp:TextBox>

Then create an onTextChanged event handler to do the same thing as your button. Or you could just point it to the same event handler your button has

清君侧 2024-10-28 05:03:31

看看 onclientitemselected

这里有一些信息:
使用 AutoCompleteExtender 控件实现自动建议

Have a look at onclientitemselected

There is some info here:
Implementing Auto-Suggest Using AutoCompleteExtender Control

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文