Gridview ASP.NET 根据同一列中第一个下拉列表的选定值更改第二个下拉列表的值

Gridview ASP.NET Changing values of the second drop down list based on value selected of first drop down in same column

<asp:SqlDataSource  ID="sdsUsers" runat="server"
ConnectionString="<%$ ConnectionStrings:Org Name %>"
SelectCommand="select full_name, user_id from dbo.Users where entity_state_id = 1">
</asp:SqlDataSource>
<asp:SqlDataSource  ID="sdsOrganizations"
runat="server"
ConnectionString="<%$ ConnectionStrings:Org Name %>"
SelectCommand="select organization_name from dbo.Organizations where Organization_Name like '%CDM%'">
</asp:SqlDataSource>      



<asp:TemplateField HeaderText="Status" SortExpression="type">
<ItemTemplate>
<%# Eval("Status")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned To" SortExpression="type">
<ItemTemplate>
<asp:DropDownList ID="ddlOrganization" runat="server" DataSourceID="sdsOrganizations" 

DataTextField="organization_name" AutoPostBack="true" AppendDataBoundItems="true">

下午好,我需要根据他们 select 在 gridview 中的组织从数据库中填充用户列表。

我该怎么做?

谢谢

好的,假设网格是这样的(有两个组合框)。

在这个例子中,我们有一些人,我们希望他们去 select 城市,然后 select 给定城市的酒店(所以我们从城市 cbo 级联到酒店列表第二个组合)。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="ID" CssClass="table" Width="30%" >
    <Columns>
        <asp:BoundField DataField="Firstname" HeaderText="Firstname" />
        <asp:BoundField DataField="LastName" HeaderText="LastName"  />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:TemplateField HeaderText="Select Hotel City">
            <ItemTemplate>
                <asp:DropDownList ID="cboCity" runat="server" Width="120px"
                    DataTextField = "City"
                    DataValueField = "City"
                    AutoPostback="true"
                    OnSelectedIndexChanged="cboCity_SelectedIndexChanged"  > 
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Select Hotel">
            <ItemTemplate>
                <asp:DropDownList ID="cboHotels" runat="server" Width="210px"
                    DataValueField ="ID"
                    DataTextField ="HotelName"
                    ></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我们要加载的代码是这样的:

Dim rstCity As New 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()

    ' load up City list for combo box
    rstCity = MyRst("SELECT City from City ORDER BY City")

    ' load up the grid
    GridView1.DataSource = MyRst("SELECT * from People ORDER BY FirstName")
    GridView1.DataBind()

End Sub

Public Function MyRst(strSQL As String) As DataTable

    Dim rstData As New DataTable

    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
        End Using
    End Using

    Return rstData

End Function

我们的结果是这样的:

好的,我们的第一个城市 cbo 框有了这个:

<asp:TemplateField HeaderText="Select Hotel City">
            <ItemTemplate>
                <asp:DropDownList ID="cboCity" runat="server" Width="120px"
                    DataTextField = "City"
                    DataValueField = "City"
                    AutoPostback="true"
                    OnSelectedIndexChanged="cboCity_SelectedIndexChanged"  > 
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>

因此,在这个组合的索引更改事件中,在我们 select 城市之后,我们用该城市的酒店填充第二个组合框。

该代码如下所示:

Protected Sub cboCity_SelectedIndexChanged(sender As Object, e As EventArgs)

    Dim cboCity As DropDownList = sender
    ' get current grid row
    Dim gRow As GridViewRow = cboCity.NamingContainer

    ' filter hotels to current city
    Dim strCity As String = cboCity.SelectedItem.Text

    Dim strSQL = "SELECT * from tblHotels WHERE CITY = '" & strCity & "' ORDER BY HotelName"
    Dim cboHotels As DropDownList = gRow.FindControl("cboHotels")
    cboHotels.DataSource = MyRst(strSQL)
    cboHotels.DataBind()
    cboHotels.Items.Insert(0, New ListItem("", ""))

End Sub

因此,在更改 cbo 时,我们获取当前行,获取第二个 cbo 框,然后仅根据城市的第一个 cbo 框填写城市酒店列表

我们当然可以(应该添加)更多活动部件。

例如,当我们保存酒店名称(cbo 框)时,我们不保存城市值 - 我们不必保存。

然而,这意味着在第一个电网负载上,理论上我们必须:

检查酒店是否已加载,如果是,则根据 selection 设置第一个组合框。 (或者我们可以在启动时将所有城市 cbo 留空 - 但理论上,我们应该检查酒店,如果是,则获取酒店的城市,然后设置城市 cbo 框。这实际上比上面的级联代码,因为我们必须查询酒店来获取城市,然后以此为基础设置城市cbo box。

但是,这就是代码 - 我们把它放在行数据绑定事件中,因为在第一次 gv 加载时,我们必须设置城市 cbo 框(它没有绑定到任何数据 - 并注意保存在数据库中 - 它只是一个 selector 过滤 hte hotel selection(保存在数据库中)。

因此,该代码 - 有点乱,但理论上是必需的。

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        ' get the data bind row data
        Dim gData As DataRowView = e.Row.DataItem
        ' load the city combo box - full city list
        Dim cboCity As DropDownList = e.Row.FindControl("cboCity")

        cboCity.DataSource = rstCity
        cboCity.DataBind()

        ' add blank row for city
        cboCity.Items.Insert(0, New ListItem("", ""))

        ' now load Hotel combo box (if we have one!!)
        Dim cboHotels As DropDownList = e.Row.FindControl("cboHotels")

        If Not IsDBNull(gData("Hotel_id")) Then

            ' get the one hotel record - we need the city from that hotel
            ' to set the city cbo

            Dim rstOneHotel As DataRow = MyRst("SELECT * From tblHotels where ID = " & gData("Hotel_id")).Rows(0)
            Dim strHotelCity As String = rstOneHotel("City")

            ' set the city filter cbo to correct city
            cboCity.SelectedValue = strHotelCity

            ' load hotel combo box only with current city list
            Dim strSQL As String = "Select ID,HotelName From tblHotels WHERE City = '" &
                                    strHotelCity & "' ORDER BY HotelName"
            cboHotels.DataSource = MyRst(strSQL)
            cboHotels.DataBind()
            ' set hotels combo to current selected
            cboHotels.SelectedValue = gData("Hotel_id")
        End If

    End If

End Sub