在 DropDownList 中启用数据绑定列表项
我有一个在DetailsView 的EditItemTemplate 中使用的下拉列表,它是从SqlDataSource 填充的,并且我按如下方式绑定所选值:
<EditItemTemplate>
<asp:DropDownList ID="lstLocations" runat="server"
DataSourceID="sqlMDALocationsAll" DataTextField="loc_name" DataValueField="id"
SelectedValue='<%# Bind("staff_location_id") %>' AppendDataBoundItems="True" >
<asp:ListItem Value="">(Unknown)</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
一切按预期工作。现在我想做的是仅启用基于 sqlDataSource 中另一列的绑定列表项 - 有一个“状态”列,可以具有活动或不活动的值 - 如果条目的状态是活动的,那么我想要启用相应的列表项,否则我希望禁用它。原因是,由于这是一个编辑表单,我不希望人们能够选择不活动的值,但我需要在下拉列表中包含这些“不活动”条目,因为主条目是正在编辑的位置很可能有一个现在处于非活动状态的位置 ID。
我尝试使用的是以下针对 DropDownList 定义的内容:
Enabled='<%# Eval("status") = "active" %>'
但这不起作用 - 但没有报告任何错误。
有什么建议吗?
谢谢
I have a drop down list that is being used in an EditItemTemplate of a DetailsView, and it is being populated from a SqlDataSource, and I am binding the selected value as follows:
<EditItemTemplate>
<asp:DropDownList ID="lstLocations" runat="server"
DataSourceID="sqlMDALocationsAll" DataTextField="loc_name" DataValueField="id"
SelectedValue='<%# Bind("staff_location_id") %>' AppendDataBoundItems="True" >
<asp:ListItem Value="">(Unknown)</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
All works as expected. Now what I want to do is enable only those bound list items based on another column in the sqlDataSource - there is a "status" column that can have values of either active or inactive - and if the status of an entry is active, then I want the corresponding list item to be enabled, otherwise I want it to be disabled. The reason is that since this is an edit form, I don't want people to be able to select a value that is inactive, but I need to include those "inactive" entries in the drop down list, since the main entry that is being edited could well have a location id for a location that is now inactive.
What I tried to use was the following atted to the DropDownList definition:
Enabled='<%# Eval("status") = "active" %>'
But that didn't work - but there weren't any errors reported.
Any suggestions?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法在 Web 控件和数据控件(如 DetailsView)内执行后期绑定评估。
在 ItemDataBound 上分配值。查看这个类似的问题。
You cannot perform a late-bound evaluation inside a webcontrol and within a data control like DetailsView.
Assign the value on ItemDataBound. Check out this similar question.
嗯,我发现了两件事。首先,也是最重要的一点是,.Net 不允许您禁用下拉列表控件中的列表项。第二个是我确实必须接受 okw 的建议并使用事件处理程序 - 我选择使用 onbadabound 事件:
最初,行 lstLocations.Items(nIndex).Text &= " (Unavailable)" 实际上设置了“enabled” " 该列表项的属性为 false,但唯一的效果是将列表项从下拉列表中完全删除。
Well, I discovered two things. The first, and most important, is that .Net doesn't allow you to disable listitems in a dropdownlist control. The second is that I really had to take o.k.w.'s suggestion and use an event handler - I chose to use an onbadabound event:
Originally, the line lstLocations.Items(nIndex).Text &= " (Unavailable)" actually set the "enabled" property of that listitem to false, but the only effect of that was dropping the listitem completely from the dropdown list.