在DropDownList中启用数据绑定列表项

时间:2009-10-02 14:05:30

标签: asp.net late-bound-evaluation

我有一个正在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中的另一列启用那些绑定列表项 - 有一个“status”列可以具有活动或非活动的值 - 如果条目的状态是活动的,那么我想要启用相应的列表项,否则我希望它被禁用。原因是,由于这是一个编辑表单,我不希望人们能够选择不活动的值,但我需要在下拉列表中包含那些“非活动”条目,因为主条目是正在编辑的对于现在处于非活动状态的位置可能具有位置ID。

我尝试使用的是下面的DropDownList定义:

Enabled='<%# Eval("status") = "active" %>'

但这不起作用 - 但没有报告任何错误。

有什么建议吗?

由于

2 个答案:

答案 0 :(得分:1)

您无法在Web控件内和DetailsView等数据控件中执行后期绑定评估。

在ItemDataBound上分配值。看看这个类似的question

答案 1 :(得分:0)

好吧,我发现了两件事。第一个也是最重要的是.Net不允许你在下拉列表控件中禁用列表项。第二个是我真的不得不采取o.k.w.的建议并使用事件处理程序 - 我选择使用onbadabound事件:

    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

最初,行lstLocations.Items(nIndex).Text&amp; =“(Unavailable)”实际上将该listitem的“enabled”属性设置为false,但唯一的影响是将listitem完全从下拉列表中删除列表。

相关问题