Asp.net 在DropDownList中启用数据绑定列表项
我在DetailsView的EditItemTemplate中使用了一个下拉列表,它是从SqlDataSource填充的,我正在绑定所选的值,如下所示:Asp.net 在DropDownList中启用数据绑定列表项,asp.net,late-bound-evaluation,Asp.net,Late Bound Evaluation,我在DetailsView的EditItemTemplate中使用了一个下拉列表,它是从SqlDataSource填充的,我正在绑定所选的值,如下所示: <EditItemTemplate> <asp:DropDownList ID="lstLocations" runat="server" DataSourceID="sqlMDALocationsAll" DataTextField="loc_name" DataValueField="id"
<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中的另一列启用那些绑定的列表项—有一个“状态”列,其值可以为active或inactive—如果某个条目的状态为active,则我希望启用相应的列表项,否则我希望将其禁用。原因是,由于这是一个编辑表单,我不希望人们能够选择一个非活动的值,但我需要在下拉列表中包括那些“非活动”条目,因为正在编辑的主条目很可能有一个当前非活动位置的位置id
我尝试在DropDownList定义中使用以下内容:
Enabled='<%# Eval("status") = "active" %>'
Enabled=''
但这不起作用——但没有任何错误报告
有什么建议吗
谢谢您不能在网络控件和数据控件(如DetailsView)中执行后期绑定评估
在ItemDataBound上指定值。看看这个类似的例子。我发现了两件事。首先,也是最重要的一点是.Net不允许您在dropdownlist控件中禁用listitems。第二个是我真的必须接受o.k.w.的建议并使用事件处理程序-我选择使用OnBadBillouge事件:
Protected Sub lstLocations_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
'Once the drop down list has been populated, we want to disable any locations whose status is inactive.
Dim lstLocations As DropDownList = CType(Me.dgStaff.FindControl("lstLocations"), DropDownList)
If Not lstLocations Is Nothing Then
Dim dView As System.Data.DataView = CType(Me.sqlMDALocationsAll.Select(DataSourceSelectArguments.Empty), System.Data.DataView)
If Not dView Is Nothing Then
dView.Sort = "id"
For nIndex As Integer = 0 To lstLocations.Items.Count - 1
If lstLocations.Items(nIndex).Value <> "" Then
Dim rowIndex As Integer = dView.Find(CInt(lstLocations.Items(nIndex).Value))
Trace.Write("lstLocations_DataBound", "Location ID = " & lstLocations.Items(nIndex).Value & " Name = " & dView(rowIndex)("loc_name") & " Status = " & dView(rowIndex)("status"))
If dView(rowIndex)("status").ToString.ToLower.Trim = "inactive" Then
lstLocations.Items(nIndex).Text &= " (Unavailable)"
End If
End If
Next
End If
Else
Trace.Write("lstLocations_DataBound", "FindControl failed")
End If
End Sub
Protected Sub lstLocations\u数据绑定(ByVal sender作为对象,ByVal e作为System.EventArgs)
'填充下拉列表后,我们希望禁用任何状态为非活动的位置。
Dim lstLocations As DropDownList=CType(Me.dgStaff.FindControl(“lstLocations”),DropDownList)
如果不是,那就什么都不是了
Dim dView As System.Data.DataView=CType(Me.sqlMDALocationsAll.Select(DataSourceSelectArguments.Empty),System.Data.DataView)
如果不是,那么dView什么都不是
dView.Sort=“id”
对于整数形式的nIndex=0到lstLocations.Items.Count-1
如果lstLocations.Items(nIndex.Value)为“”,则
Dim rowIndex As Integer=dView.Find(CInt(lstLocations.Items(nIndex.Value))
Trace.Write(“lstLocations_DataBound”,“Location ID=”&lstLocations.Items(nIndex)。Value&“Name=”&dView(rowIndex)(“loc_Name”)和“Status=”&dView(rowIndex)(“Status”))
如果dView(rowIndex)(“status”).ToString.ToLower.Trim=“非活动”,则
lstLocations.Items(nIndex).Text&=(不可用)
如果结束
如果结束
下一个
如果结束
其他的
Trace.Write(“lstLocations\u数据绑定”,“FindControl失败”)
如果结束
端接头
最初,行lstLocations.Items(nIndex.Text&=“(不可用)”实际上将该listitem的“enabled”属性设置为false,但唯一的效果是将listitem完全从下拉列表中删除。感谢您提供的信息-这并没有真正起到帮助,我想我把自己弄糊涂了,因为涉及到两个数据源——一个用于详细信息视图,另一个用于填充下拉列表中的列表项成员。我需要解决的是,我如何能够以某种方式为从数据源添加到下拉列表中的每个列表项捕获一个“onDataBound”。但是onDataBinding事件似乎没有公开我需要的值。您仍然可以通过附加事件处理程序来执行数据处理任务,将detailsview中的dropdownlist视为普通控件。