似乎无法访问存储在数据表中的下拉列表

问题描述 投票:0回答:1

这需要一些解释。

我有一个带有 2 个下拉列表的 asp.net 页面。这些都是回发的。我使用 vb.net 作为后面的代码。然后我有一个公共数据表,其中存储一些与下拉列表相关的值。

表格(tblfilters)看起来像这样:

索引 数据库字段 DD列表
0 购买年份 ddlYOP
1 设置编号 ddl号码

Index 数据类型是整数,DB Field 是字符串,DDList 是 dropdownlist

我可以循环遍历表记录,并使用 DDList 将值绑定到 SQL 中的控件,效果很好。我的问题是我似乎无法从相同的下拉列表中获取 Selecteditem.Text 。如果我通过 ID 引用页面上的控件,那么我会获取所选值,但是如果我尝试使用数据表字段值,则无论选择什么,我得到的都是下拉列表中的顶部值。

下拉列表的绑定仅发生一次并且已经在 page_load 事件中

If Not IsPostBack Then
    For Each row In tblfilters.Rows
        BindFilterData(row.Item("DBField"), row.Item("DDList"))
    Next
End If

我尝试通过以下方法引用 DDList 值:

For Each row In tblfilters.Rows
    lbloutput.Text += row.Item("DDList").Selecteditem.Text
Next

For Each row In tblfilters.Rows
    Dim ddltest as dropdownlist = FindControl(row.Item("DDList"))
    lbloutput.Text += ddltest.Selecteditem.Text
Next

无论我是否在下拉列表中选择了任何内容,我总是只取回列表中的第一个值。

正如我所说,如果我使用以下内容,那么它会起作用,但我确实可以循环遍历数据表来完成我想要做的事情:

lbloutput.Text += ddlyp.SelectedItem.Text
lbloutput.Text += ddlnumber.SelectedItem.Text

我确信我在这里遗漏了一些明显的东西,希望有人能够发现我的方法的错误。

提前致谢

asp.net vb.net datatable drop-down-menu
1个回答
0
投票

好的,在 Web 表单中显示表格的一般步骤是:

将组合框选项加载到每个组合框的数据表中。

加载表格的显示。这可以是 GridView、Listview、Repeater,甚至是较旧的数据网格(您不再在工具箱中找到数据网格,但在标记中可以使用它们)。

对于表格的显示,通常 GridView 是一个不错的选择,但是随着网格显示中的标记和自定义控件越来越多,那么我倾向于转向使用 Listview,因为它的布局更灵活以及使用组合框 (ddl) 等控件。

因此,对于这个示例,我们将有一个人员列表,对于每一行数据,我们将有一个用于城市的组合框(数据库中的简单文本列),以及第二个用于酒店选择的组合框。在第二个组合框中,当然我们将存储/保存/使用酒店选择的 PK,但显示 HotelName(因此,组合框有 2 个值,酒店的隐藏 PK 值,然后是 HotelName 的文本值)。选择酒店组合框后,我们需要将所选酒店的 PK 值保存到一个数据行中。

因此,在加载列表视图后,我们必须加载组合框,然后需要一个额外的步骤将当前网格行组合框设置为为该行选择的正确的基本值。

所以,说出这个标记:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID"
    OnItemDataBound="ListView1_ItemDataBound">
    <LayoutTemplate>
        <table id="itemPlaceholderContainer" runat="server" border="0" 
            class="table table-bordered table-hover">
            <tr runat="server" style="">
                <th runat="server" style="width:100px">First</th>
                <th runat="server" style="width:100px">Last</th>
                <th runat="server" style="width:160px" >City</th>
                <th runat="server" style="width:240px">Hotel Name</th>
            </tr>
            <tr id="itemPlaceholder" runat="server">
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr style="">
            <td><asp:Label ID="First" runat="server" Text='<%# Eval("FirstName") %>' /></td>
            <td><asp:Label ID="Last" runat="server" Text='<%# Eval("LastName") %>' /></td>
            <td>
                <asp:DropDownList ID="cboCity" runat="server"
                    DataTextField="City"
                    width="100%"
                    >
                </asp:DropDownList>
            </td>
            <td>
                <asp:DropDownList ID="cboHotel" runat="server"
                    DataTextField="HotelName"
                    DataValueField="ID"
                    width="100%"
                    >
                </asp:DropDownList>
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>

所以,请注意上面如何有 2 个组合框(城市和酒店)

背后的代码是这样的:

Dim dtCityList As DataTable
Dim dtHotelList As DataTable

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadGrid
    End If
End Sub


Sub LoadGrid()

    dtCityList = MyRst("SELECT City FROM City ORDER BY City")
    dtHotelList = MyRst("SELECT ID, HotelName FROM tblHotels ORDER BY HotelName")

    ListView1.DataSource = MyRst("SELECT * FROM People ORDER BY LastName")
    ListView1.DataBind()

End Sub


Protected Sub ListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs)

    If e.Item.ItemType = ListViewItemType.DataItem Then

        Dim dRow As DataRowView = e.Item.DataItem    ' get binding data source

        Dim cboCity As DropDownList = e.Item.FindControl("cboCity")
        cboCity.DataSource = dtCityList
        cboCity.DataBind()
        cboCity.Items.Insert(0, "Select City")
        If Not IsDBNull(dRow("City")) Then
            cboCity.Text = dRow("City")
        End If

        Dim cboHotels As DropDownList = e.Item.FindControl("cboHotel")
        cboHotels.DataSource = dtHotelList
        cboHotels.DataBind()
        cboHotels.Items.Insert(0, "Select Hotel")
        If Not IsDBNull(dRow("Hotel_ID")) Then
            cboHotels.Text = dRow("Hotel_ID")
        End If
    End If

End Sub

现在的结果是这样的:

所以,上面的重要部分?

我们使用每行的组合框选择加载 2 个数据表对象。

然后我们加载网格(列表视图),并在项目数据绑定上(针对每行运行),然后加载每个组合框(使用查找控件获取当前行的引用)。然后,我们加载每个组合框,并将当前选定的组合框值设置为给定数据行的当前基础值。

© www.soinside.com 2019 - 2024. All rights reserved.