在 ASP.net Bind() 调用上测试 null、内联
我开始管理一些代码,但由于数据库中缺少一些数据,它开始失败。这种情况将来可能会发生,所以我想在前端优雅地处理空值。
这是当前的代码:
<asp:DropDownList ID="ddlContact" runat="server"
SelectedIndex='<%# Bind("contactInfo") == null ? "" : Bind("contactInfo") %>'>
它似乎对它没有任何影响,页面仍然抛出 NullReferenceException。由于双向数据绑定要求,它需要是 Bind(),所以我不能使用 Eval()。有什么想法吗?
我尝试使用空合并运算符“??”但这给了我一个编译错误,指出当前上下文中不存在 Bind() 。那可能看起来像这样:
<asp:DropDownList ID="ddlContact" runat="server"
SelectedIndex='<%# Bind("contactInfo") ?? string.Empty %>'>
I've got a bit of code that I started to manage, and it's begun to fail due to some data missing in the database. This case could happen in the future, so I'd like to gracefully handle the nulls in the front end.
Here's the current code:
<asp:DropDownList ID="ddlContact" runat="server"
SelectedIndex='<%# Bind("contactInfo") == null ? "" : Bind("contactInfo") %>'>
It doesn't seem to have any affect on it, and the page still throws a NullReferenceException. It needs to be a Bind() due to the two-way data binding requirement, so I can't use Eval(). Any ideas?
I've tried to use the null-coallescing operator "??" but that gives me a compilation error stating that Bind() does not exist in the current context. That could would look like this:
<asp:DropDownList ID="ddlContact" runat="server"
SelectedIndex='<%# Bind("contactInfo") ?? string.Empty %>'>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查这个:
绑定 NULL
这个应该会给你更多的想法:
如何处理 DataBinder.Eval( )
使用数据源控件处理空数据库值
Check this one:
Bind NULL
This one should give you more ideas:
How to handle null values in DataBinder.Eval()
Handling Null Database Values Using Data Source Controls