【问题标题】:Enabling Databound List Items in DropDownList在 DropDownList 中启用数据绑定列表项
【发布时间】:2009-10-06 21:53:23
【问题描述】:

我有一个在 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" %>'

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

有什么建议吗?

谢谢

【问题讨论】:

    标签: asp.net late-bound-evaluation


    【解决方案1】:

    您不能在 web 控件和像 DetailsView 这样的数据控件中执行后期绑定评估。

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

    【讨论】:

    • 感谢您提供的信息 - 它并没有真正帮助,我想我设法让自己混淆了涉及两个数据源 - 一个用于详细信息视图,另一个用于填充列表项成员下拉列表。我需要解决的是如何以某种方式为每个从其数据源添加到下拉列表的列表项捕获一个“onDataBound”。但是 onDataBinding 事件似乎没有公开我需要的值。
    • 您仍然可以通过附加事件处理程序来执行数据任务,将详细信息视图中的下拉列表视为普通控件。
    【解决方案2】:

    嗯,我发现了两件事。首先,也是最重要的一点,.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 &= " (Unavailable)" 实际上将该列表项的“启用”属性设置为 false,但唯一的效果是从下拉列表中完全删除列表项.

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      相关资源
      最近更新 更多