Method and implementation code for obtaining row index of gridview

Source: Internet
Author: User

Insus. NET to demonstrate this example, first prepare the data and create a category
Cosmetic. vb
Copy codeThe Code is as follows:
Imports Microsoft. VisualBasic
Namespace Insus. NET
Public Class Cosmetic
Private _ ID As Integer
Private _ Type As String
Private _ Name As String
Private _ Weight As Decimal
Private _ UM As String
Public Property ID As Integer
Get
Return _ ID
End Get
Set (value As Integer)
_ ID = value
End Set
End Property
Public Property Type As String
Get
Return _ Type
End Get
Set (value As String)
_ Type = value
End Set
End Property
Public Property Name As String
Get
Return _ Name
End Get
Set (value As String)
_ Name = value
End Set
End Property
Public Property Weight As Decimal
Get
Return _ Weight
End Get
Set (value As Decimal)
_ Weight = value
End Set
End Property
Public Property UM As String
Get
Return _ UM
End Get
Set (value As String)
_ UM = value
End Set
End Property
Public Sub New ()
End Sub
Public Sub New (id As Integer, type As String, name As String, weight As Decimal, um As String)
Me. _ ID = id
Me. _ Type = type
Me. _ Name = name
Me. _ Weight = weight
Me. _ UM = um
End Sub
End Class
End Namespace

The object created above is only an object. You need to fill it with data so that it is flesh, blood, and soul.
Copy codeThe Code is as follows:
Private Function GetData () As List (Of Cosmetic)
Dim o As New List (Of Cosmetic)
Dim c As New Cosmetic (1, "moisturizing cream", "Olay", 50, "g ")
O. Add (c)
Dim c1 As New Cosmetic (2, "moisturizing cream", "Estee Lauder", 100, "g ")
O. Add (c1)
Dim c2 As New Cosmetic (3, "moisturizing cream", "Lancome", 80, "g ")
O. Add (c2)
Dim c3 As New Cosmetic (4, "moisturizing cream", "l'oreal", 60, "g ")
O. Add (c3)
Dim c4 As New Cosmetic (5, "moisturizing cream", "Babi polang", 120, "g ")
O. Add (c4)
Return o
End Function

Put a Gridview control on the aspx webpage:
Copy codeThe Code is as follows:
<Asp: GridView ID = "GridViewCosmetic" runat = "server" Width = "300" AutoGenerateColumns = "false">
<Columns>
<Asp: TemplateField>
<HeaderTemplate>
ID
</HeaderTemplate>
<ItemTemplate>
<% # Eval ("ID") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<HeaderTemplate>
Type
</HeaderTemplate>
<ItemTemplate>
<% # Eval ("Type") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<% # Eval ("Name") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<ItemStyle HorizontalAlign = "Right"/>
<HeaderTemplate>
Weight
</HeaderTemplate>
<ItemTemplate>
<% # Eval ("Weight") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<HeaderTemplate>
UM
</HeaderTemplate>
<ItemTemplate>
<% # Eval ("UM") %>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>

Of course, you must bind data to this control and reference the namespace Imports Insus. NET.
Copy codeThe Code is as follows:
Protected Sub Page_Load (sender As Object, e As EventArgs) Handles Me. Load
If Not IsPostBack Then
Data_Binding ()
End If
End Sub
Private Sub Data_Binding ()
Me. GridViewCosmetic. DataSource = GetData ()
Me. GridViewCosmetic. DataBind ()
End Sub

Next, we will show you how to add a column in the last column of the GridView control and select the column:
Copy codeThe Code is as follows:
<Asp: TemplateField>
<ItemTemplate>
<Asp: LinkButton ID = "LinkButton1" runat = "server" Text = "select" OnClientClick = "return GetSelectedRow (this)"/>
</ItemTemplate>
</Asp: TemplateField>

In the preceding html code, there is an OnClientClick = "return GetSelectedRow (this)" client event.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function GetSelectedRow (obj ){
Var row = obj. parentNode. parentNode;
Var rowIndex = row. rowIndex-1;
Alert ("the row index you selected is:" + rowIndex );
Return false;
}
</Script>

Animation demonstration:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.