如何设置 AutoCompleteExtender 以仅允许自动完成列表中的值?
有谁知道如何使用 AutoCompleteExtender(来自 AJAX Control Toolkit)防止用户输入建议值之外的任何内容?
Does anybody know how using AutoCompleteExtender (from AJAX Control Toolkit) prevent user from entering anything not in suggested values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,检查您是否愿意使用新的 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).
像这样的东西可以帮助你
Javascript
ASPX
Something like this can help you
Javascript
ASPX
如果用户最初从列表中选择一个项目,然后返回并决定键入一个值,则先前使用布尔值
isItemSelected
提交的答案将无法工作。为了防止这种情况,还应该有一个事件,当输入获得焦点时将 isItemSelected 重置为 false:
ASPX
JS
Or...
使用选择时触发的 JS 事件,并将值从文本框复制到隐藏字段。然后使用隐藏字段中的值进行处理。
ASPX
JS
但是等等...还有更多!
为了增强上述功能,您可以向文本框添加一个模糊事件来验证是否文本框值与隐藏字段中的值匹配,当值不匹配时清除文本框。
这将使用户清楚地知道输入未被接受。
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
JS
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
JS
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.
This will make it obvious to the user that the input is not being accepted.