如何设置 AutoCompleteExtender 以仅允许自动完成列表中的值?

发布于 2024-08-08 14:22:19 字数 78 浏览 4 评论 0原文

有谁知道如何使用 AutoCompleteExtender(来自 AJAX Control Toolkit)防止用户输入建议值之外的任何内容?

Does anybody know how using AutoCompleteExtender (from AJAX Control Toolkit) prevent user from entering anything not in suggested values?

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

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

发布评论

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

评论(3

倦话 2024-08-15 14:22:19

首先,检查您是否愿意使用新的 AjaxToolKit ComboBox。

如果您不能(例如,如果您使用的是 .NET Framework 2.0),您可以操纵自动完成来满足您的需求,但这很令人头痛,而且并不是控件的真正用途。

检查应该在 javascript 内部进行,您添加一个事件来捕获 OnItemSelected。
然后创建一个函数:

函数 OnItemSelected (sender, e)
 {
 -- 在此验证
 }

另一种选择是要求用户通过操作事件从列表中选择值:
onchange、onclick 和模糊。但找到合适的组合需要一些时间。

为了让您振作起来,我会告诉您这是可能的(我们已经做到了,但由于版权问题我无法附上我们的代码)。

First, check if you would rather use the new AjaxToolKit ComboBox.

If you can't (for example, if you're using .NET Framework 2.0), you can manipulate the AutoComplete to answer your demands but it's a headache and not really what the control was made to.

The checks are supposed to be made inside the javascript, you add an event to catch OnItemSelected.
And then create a function:

 function OnItemSelected (sender, e)
 {
 -- validate here
 }

Another option is to require user to choose value from list by manipulating the events:
onchange, onclick & onblur. But it takes some time to find just right combination.

To lift your spirits I'll tell you that it is possible (we've done it, but I can't attach our code because of copyright issues).

夜未央樱花落 2024-08-15 14:22:19

像这样的东西可以帮助你

Javascript

<script type="text/javascript">
        var isItemSelected = false;

        //Handler for AutoCompleter OnClientItemSelected event
        function onItemSelected() {
            isItemSelected = true;
        }

        //Handler for textbox blur event
        function checkItemSelected(txtInput) {
            if (!isItemSelected) {
                alert("Only choose items from the list");
                txtInput.focus();
            }
        }
</script>

ASPX

<asp:Button onblur="checkItemSelected(this)" ../>
<ajax:AutoCompleteExtender OnClientItemSelected="onItemSelected" ../>

Something like this can help you

Javascript

<script type="text/javascript">
        var isItemSelected = false;

        //Handler for AutoCompleter OnClientItemSelected event
        function onItemSelected() {
            isItemSelected = true;
        }

        //Handler for textbox blur event
        function checkItemSelected(txtInput) {
            if (!isItemSelected) {
                alert("Only choose items from the list");
                txtInput.focus();
            }
        }
</script>

ASPX

<asp:Button onblur="checkItemSelected(this)" ../>
<ajax:AutoCompleteExtender OnClientItemSelected="onItemSelected" ../>
拥抱我好吗 2024-08-15 14:22:19

如果用户最初从列表中选择一个项目,然后返回并决定键入一个值,则先前使用布尔值 isItemSelected 提交的答案将无法工作。

为了防止这种情况,还应该有一个事件,当输入获得焦点时将 isItemSelected 重置为 false:

ASPX

<asp:TextBox onblur="checkItemSelected(this)" onfocus="resetItemSelected()"../>

JS

function resetItemSelected() {
     isItemSelected = false;
}

Or...

使用选择时触发的 JS 事件,并将值从文本框复制到隐藏字段。然后使用隐藏字段中的值进行处理。

ASPX

<asp:HiddenField runat="server" ID="hf1"/>
<asp:TextBox runat="server" ID="tb1"></asp:TextBox>
<ajax:AutoCompleteExtender ID="ace1" runat="server" TargetControlID="tb1" OnClientItemSelected="userSelected" .../>

JS

function userSelected(sender, e) {
    var selectedItem = e.get_value();
    $get("<%= hf1.ClientID%>").value = selectedItem;
    return false;
}

但是等等...还有更多!

为了增强上述功能,您可以向文本框添加一个模糊事件来验证是否文本框值与隐藏字段中的值匹配,当值不匹配时清除文本框。

//remove value if not selected from drop down list
$('#<%=tb1.ClientID%>').blur(function () {
    if ($('#<%=hf1.ClientID%>').val() !== $(this).val()) {
        $(this).val("");
        //optionally add a message near the input
    }
});

这将使用户清楚地知道输入未被接受。

The previously submitted answer using a boolean isItemSelected will fail to work if a user initially selects an item from the list, and then goes back and decides to type a value instead.

In order to prevent this, there should also be an event that resets isItemSelected to false when the input comes to focus:

ASPX

<asp:TextBox onblur="checkItemSelected(this)" onfocus="resetItemSelected()"../>

JS

function resetItemSelected() {
     isItemSelected = false;
}

Or...

Use a JS event that is triggered on select, and copy the value from the textbox to a hidden field. Then use the value from the hidden field for processing.

ASPX

<asp:HiddenField runat="server" ID="hf1"/>
<asp:TextBox runat="server" ID="tb1"></asp:TextBox>
<ajax:AutoCompleteExtender ID="ace1" runat="server" TargetControlID="tb1" OnClientItemSelected="userSelected" .../>

JS

function userSelected(sender, e) {
    var selectedItem = e.get_value();
    $get("<%= hf1.ClientID%>").value = selectedItem;
    return false;
}

But wait... there's more!

To enhance the above, you can add a blur event to the textbox that validates whether or not the textbox value matches the value in the hidden field, and clears the textbox when the value does not match.

//remove value if not selected from drop down list
$('#<%=tb1.ClientID%>').blur(function () {
    if ($('#<%=hf1.ClientID%>').val() !== $(this).val()) {
        $(this).val("");
        //optionally add a message near the input
    }
});

This will make it obvious to the user that the input is not being accepted.

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