如果值不存在,则将 DropDownList 的 SelectedIndex 绑定到 0
我在DetailsView 中有一个DropDownList,它将用户的“已分配用户”绑定到“用户”表中的用户。有时,我们会在数据库中遇到一些问题,分配的用户不在 DropDownList 填充的用户表中。在这种情况下,网站崩溃,我们收到“项目列表中不存在 SelectedValue 错误”。
我想要做的是捕获任何类似的绑定错误,然后将 DropDownList 索引设置为 0。我尝试过类似的操作:
OnDataBinding='DoesExist("AllocatedUser",<%#Bind("AllocatedUser")%>)'
但它不喜欢这样的语法。还有其他想法吗?
I have a DropDownList in a DetailsView, which binds a user's 'AllocatedUser' to a user in the Users table. Occasionally, we'll have some mishaps in the database where the user allocated isn't in the users table, which the DropDownList is filled with. In this case the site crashes and we get a 'SelectedValue does not exist in the list of items error'.
What I want to do is catch any binding errors like that, and just set the DropDownList index to 0. I've tried something like this:
OnDataBinding='DoesExist("AllocatedUser",<%#Bind("AllocatedUser")%>)'
but it doesn't like the syntax of that. Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题通常会被一次又一次地问到,无法从数据库中找到一个非常好的旧问题,其中包含一些
SELECT 0 IF NULL
或类似的问题,但找到了这个应该有帮助的问题...< a href="https://stackoverflow.com/questions/741025/how-to-bind-to-a-dropdown-list-wih-no-value">如何绑定到没有值的下拉列表
your question is usually asked again and again, couldn't find an old one really nice with some
SELECT 0 IF NULL
from the database, or similar, but found this one which should help anyway...How to bind to a dropdown list with no value