如何获取动态创建的asp.net GridView的行索引?

时间:2016-10-07 11:27:55

标签: c# asp.net gridview

如何获取动态创建的asp.net GridView的行索引,以便我可以在GridView之外的文本框中编辑记录。  如何获取动态创建的asp.net GridView的行索引,以便我可以在GridView之外的文本框中编辑记录。

这是GridView

<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
  <AlternatingRowStyle BackColor="White" />
  <Columns>
    <asp:TemplateField HeaderText="RefID">
      <ItemTemplate>
        <asp:Label ID="lbl_Refid" runat="server" Text='<%# Eval("Refid") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Date">
      <ItemTemplate>
        <asp:Label ID="lbl_date" runat="server" Text='<%# Eval("Date") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name Of Company">
      <ItemTemplate>
        <asp:Label ID="lbl_noc" runat="server" Text='<%# Eval("Name_Of_Company") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Contact/Email">
      <ItemTemplate>
        <asp:Label ID="lbl_wht_do" runat="server" Text='<%# Eval("Contact") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Remarks">
      <ItemTemplate>
        <asp:Label ID="lbl_wht_do" runat="server" Text='<%# Eval("Remarks") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Edit">
      <ItemTemplate>
        <asp:Button ID="btn_edit" runat="server" `enter code here` Text="Button" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
  <EditRowStyle BackColor="#2461BF" />
  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#EFF3FB" />
  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  <SortedAscendingCellStyle BackColor="#F5F7FB" />
  <SortedAscendingHeaderStyle BackColor="#6D95E1" />
  <SortedDescendingCellStyle BackColor="#E9EBEF" />
  <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand = "OnRowCommand">

使用

OnRowCommand = "OnRowCommand"

现在,当网格上发生任何操作时,OnRowCommand事件将被执行。

protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow gvRow = GridView1.Rows[index]; 
}

More Info

答案 1 :(得分:0)

<ItemTemplate>
    <asp:Button ID="Button1" runat="server" Text="Button" 
            OnClick="MyButtonClick" />
</ItemTemplate>

和你的方法

protected void MyButtonClick(object sender, System.EventArgs e)
{
    //Get the button that raised the event
    Button btn = (Button)sender;

    //Get the row that contains this button
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;

}