【发布时间】:2018-10-10 07:53:31
【问题描述】:
我最近发现您可以在 GridView 或 DetailsView 等数据绑定控件的上下文中直接在 aspx 页面的标记中使用“BindItem”和“Item”(通过指定 ItemType 属性)。我现在想要实现的是成员的内联比较,例如:
<asp:RadioButton Text="All Assigned" ID="rb1"
Checked='<%# BindItem.AllAssigned %>'
runat="server" GroupName="AllAssigned" />
<asp:RadioButton Text="Responsible only" ID="rb2"
Checked='<%# !BindItem.AllAssigned %>'
runat="server" GroupName="AllAssigned" />
在这种情况下,我需要双向绑定,因此我选择了 BindItem 表达式。
但似乎!BindItem.AllAssigned 或BindItem.AllAssigned == false 之类的表达式在标记中不起作用。他们给了我例外,比如
名称“BindItem”在当前上下文中不存在
或
DataBinding:DataContext.MyEntity 不包含名为“false”的属性。
这样的表达我要写什么?
【问题讨论】:
-
也许你应该使用
Eval,例如<%# !(bool)Eval("BindItem.AllAssigned") %>. -
在这种情况下我需要双向绑定。
-
你想要像
Bind()这样的双向绑定吗?我认为您不能将Bind()与条件运算符一起使用,Bind()只接受字段名称。您需要处理CheckedChanged事件来检索否定值,或使用RadioButtonList处理不同的单选按钮状态。 -
我目前的方法是使用 Item 而不是 BindItem 并处理 LinqDataSource 的更新事件以确定哪个单选按钮被勾选。但这很不方便......
标签: asp.net data-binding strong-typing