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

发布于 2024-08-07 02:37:56 字数 881 浏览 1 评论 0原文

我有一个在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋心╮凉 2024-08-14 02:37:56

您无法在 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.

顾忌 2024-08-14 02:37:56

嗯,我发现了两件事。首先,也是最重要的一点是,.Net 不允许您禁用下拉列表控件中的列表项。第二个是我确实必须接受 okw 的建议并使用事件处理程序 - 我选择使用 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)" 实际上设置了“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:

    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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文