ASP.NET AJAX Toolkit Combobox:无需单击 ENTER 两次即可回发

发布于 2024-08-11 17:53:55 字数 307 浏览 2 评论 0原文

我在我的应用程序中使用 ComboBox 来实现功能,并且我有 AutoCompleteMode="Suggest"。

但是,在我在文本框中输入搜索后,我需要按 ENTER 两次才能回发并显示一些结果。这是默认行为,如官方演示。 在我看来,这有点烦人而且不直观......

有人知道如何避免这种行为,并只按一次吗?

提前致谢

im using a ComboBox for a feature in my application, and i have AutoCompleteMode="Suggest".

However, after i type in the textbox for a search, i need to press ENTER twice to postback and show some results. This is the default behavior, like its shown in the oficial demonstration.
In my opinion, its kinda annoying and not intuitive...

Anyone have a clue how to avoid this behavior, and press just one time?

Thanks in advance

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

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

发布评论

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

评论(4

ゃ懵逼小萝莉 2024-08-18 17:53:55

我遇到了同样的问题并解决了它:

如果您不希望按钮被单击两次,则必须将 autopostback 属性设置为“false”。

I faced the same problem and solved it:

You must set autopostback property to "false" if you dont want button to be clicked twice.

嗫嚅 2024-08-18 17:53:55

将控件的 AutoPostback 属性更改为 true。这将捕获 Tab 键,该键会起作用,因为控件失去焦点。

我怀疑是否有一个好方法来捕获回车键,除非您准备打开源代码并执行一些修改。

<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" 
     AutoPostBack="True" 
     DropDownStyle="DropDownList" 
     AutoCompleteMode="SuggestAppend" 
     CaseSensitive="False" 
     CssClass="" 
     ItemInsertLocation="Append" ... > 

Change the AutoPostback attribute of the control to true. This will trap the tab key, which works because the control loses focus.

I doubt there is a good way to trap the enter key for this, unless you're up for breaking open the source code and performing some modifications.

<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" 
     AutoPostBack="True" 
     DropDownStyle="DropDownList" 
     AutoCompleteMode="SuggestAppend" 
     CaseSensitive="False" 
     CssClass="" 
     ItemInsertLocation="Append" ... > 
时光与爱终年不遇 2024-08-18 17:53:55

汤姆,简是对的,我以前也遇到过这种事。你只需要将 autopostback 设置为 false 即可。因此,您可能需要将 ComboBox autopostback 设置为 false。

Tom, Jan is right and it happened to me before. you just need to set you autopostback to false. So, probably you need to set the ComboBox autopostback to false.

请你别敷衍 2024-08-18 17:53:55

我设法用下面的代码解决了这个问题:

在您的 aspx 文件中,组合框控件将是:

<ajaxToolkit:ComboBox ID="cbCountries" CssClass="AquaStyle2" runat="server" AutoPostBack="true" DropDownStyle="DropDownList"
        AutoCompleteMode="SuggestAppend" CaseSensitive="False" ItemInsertLocation="Append" onkeydown="FireEnterKey(this, event)" />

然后,添加对 javascript 文件的引用,并在其中添加以下函数:

function FireEnterKey(elem, evt) {
    var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;

    if (keyCode != 13)
        return;

        var belem = $get(elem.attributes.id.value + "_Button");  //ctl00_ContentPlaceHolder1_cbCountries_Button
        var telem = $get(elem.attributes.id.value + "_TextBox");  //ctl00_ContentPlaceHolder1_cbCountries_TextBox

        if (navigator.userAgent.search("Firefox") >= 0) {
            elem.onchange();
        }
        else if (navigator.userAgent.search("MSIE") >= 0) {
            elem.onchange();
            telem.blur();
        }
        else {  // Opera, Safari, Chrome
            telem.blur();
        }
}

我希望上面的代码能够回答您的问题。

I managed to solve this problem with the code below:

In your aspx file, the combobox control will be:

<ajaxToolkit:ComboBox ID="cbCountries" CssClass="AquaStyle2" runat="server" AutoPostBack="true" DropDownStyle="DropDownList"
        AutoCompleteMode="SuggestAppend" CaseSensitive="False" ItemInsertLocation="Append" onkeydown="FireEnterKey(this, event)" />

Then, add a reference to a javascript file, and add there the following function:

function FireEnterKey(elem, evt) {
    var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;

    if (keyCode != 13)
        return;

        var belem = $get(elem.attributes.id.value + "_Button");  //ctl00_ContentPlaceHolder1_cbCountries_Button
        var telem = $get(elem.attributes.id.value + "_TextBox");  //ctl00_ContentPlaceHolder1_cbCountries_TextBox

        if (navigator.userAgent.search("Firefox") >= 0) {
            elem.onchange();
        }
        else if (navigator.userAgent.search("MSIE") >= 0) {
            elem.onchange();
            telem.blur();
        }
        else {  // Opera, Safari, Chrome
            telem.blur();
        }
}

I hope my code above answers your question.

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