1

我创建了一个从 OracleReader 填充的 GridView。数据包含一个人的名字和姓氏。这部分工作正常。

我想添加一个 DropDownList 作为第三列,它将具有来自单独查询的数据源。我遇到的问题是在后面的代码中访问 DropDownList。另外,我将如何访问每个动态创建的 DropDown?

<asp:GridView ID="GridView_People" runat="server" emptydatatext="Make selections above">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDown_features" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                </asp:GridView>

以及在单击按钮时填充 GridView 的代码(目前一切正常)

    Protected Sub Button_Submit_Click(sender As Object, e As System.EventArgs) Handles Button_Submit.Click
    Dim Conn As OracleConnection
    Dim Cmd As OracleCommand
    Dim Reader As OracleDataReader

    Conn = New OracleConnection(--CONNECTIONSTRING--)

    Dim sqlString As String = "select first, last from TABLE"
    Cmd = New OracleCommand(sqlString)

    Cmd.Connection = Conn
    Cmd.CommandType = Data.CommandType.Text
    Try
       Conn.Open()

        Reader = Cmd.ExecuteReader()

        GridView_People.DataSource = Reader

        GridView_People.DataBind()
    Catch ex As Exception
    Finally

    End Try
    Conn.Close()
    Conn.Dispose()
End Sub

我尝试在后面的代码中访问 GridView_RowCreated 事件中的 DropDown_features,但我无法访问下拉列表。有任何想法吗?

4

1 回答 1

2

GridView RowDataBound 事件是您希望在将单个项目绑定到网格时访问它们的地方。

protected void GridView_People_RowDataBound(object sender, GridViewRowEventArgs e)
{               
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Find the particular drop down list for this row
        DropDownList ddl = (DropDownList)e.Row.FindControl("DropDown_features");

        // Go get data and do whatever you need to do to the drop down list
    }
}

注意:您需要在 GridView 的标记中为 RowDataBound 事件添加一个属性,如下所示:

onrowdatabound="GridView_People_RowDataBound"
于 2013-06-19T15:27:33.610 回答