asp. net Gets the line number of the current line in ListView and gridview

  • 2021-07-09 07:57:49
  • OfStack

This article illustrates how asp. net gets the line number of the current line in ListView and gridview. Share it for your reference, as follows:

In aspx, in gridview/ListView, there is a template column called linkbutton, and when you want to click it, get the index value of its row

In ListView:

Type 1:


<ItemTemplate>
 <tr>
  <td>
  <asp:LinkButton runat="server" ID="btnSelected" Text='<%# (Container.DisplayIndex+1).ToString() %>' ></asp:LinkButton>
   </td>
 </tr>
</ItemTemplate>

Type 2:


<ItemTemplate>
 <li>
  <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete"
   Text="<%# Container.DataItem %>"></asp:LinkButton>
 </li>
</ItemTemplate>

In gridview

Type 1:


<itemtemplate>
  <asp:LinkButton ID="LinkButton1" runat="server"
</itemtemplate>


protected void LinkButton1_Click(object sender, EventArgs e)
{
  // Line number 
  int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
}

Type 2:


<asp:GridView ID="gvTest" runat="server">
  <Columns>
  <asp:TemplateField>
  <ItemTemplate>
  DisplayIndex : <%# Container.DisplayIndex %> || DataItemIndex : <%# Container.DataItemIndex %><br />
  </ItemTemplate>
  </asp:TemplateField>
  </Columns>
</asp:GridView>

I hope this article is helpful to everyone's asp. net programming.


Related articles: