【问题标题】:Find row of an item template control in gridview在gridview中查找项目模板控件的行
【发布时间】:2014-06-06 16:24:37
【问题描述】:

我在 ASP.NET 中有一个网格视图,其中包含一个包含下拉列表的项目模板。我有一个下拉列表的事件(选定的索引已更改),我需要在其中找出该控件所在的 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?
}

【问题讨论】:

  • 尝试使用ddl.Parentddl.Parent.Parent 等,直到你到达GridViewRow。然后您应该能够使用RowIndex 属性检索它所在的行。

标签: c# asp.net


【解决方案1】:

你可以这样做,

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

【讨论】:

    【解决方案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
    

    【讨论】:

    • 那是 VB.NET,但我明白你的意思
    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多