在gridview中查找项模板控件的行

时间:2014-06-06 16:24:37

标签: c# asp.net

我在ASP.NET中有一个gridview,其中包含一个项目模板,其中包含一个下拉列表。我有一个下拉列表事件(选择的索引已更改),我需要在gridview中找出此控件所在的行。任何人都知道如何完成此操作? (我之后会添加更多行,所以这就是我需要我所在行的原因。)

<asp:gridview ID="grdTest" runat="server" ShowFooter="true" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="RowNumber" HeaderText="Query Line" />
    <asp:TemplateField HeaderText="Table Name">
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
                <asp:ListItem Value="-1">Select</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:gridview>


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // TODO: Grab the row of the control here!
    int row = 0;
    DropDownList ddl = (DropDownList)sender; // How?
}

2 个答案:

答案 0 :(得分:2)

以下代码可以帮助您

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim gvrow As GridViewRow = CType(sender, DropDownList).NamingContainer
    Dim index As Integer = CType(gvrow, GridViewRow).RowIndex
End Sub

答案 1 :(得分:2)

你可以这样做,

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  DropDownList ddl = (DropDownList) sender;
  GridViewRow row = (GridViewRow) ddl.NamingContainer;
  int rowIndex = row.RowIndex;
}