I have a dynamically created grdview with 4
columns and n
number of rows. Each column has a Button
control with different Id
s. The control has a Click
event. When the event fires I want to find the row
index and the cell
index or the Id
of that button at the run time
In this article will explain how to get the ASP.Net GridView Row and its RowIndex client side using JavaScript. I will also explain how we can find the GridView Cells and the controls inside the GridView Template Fields client side using JavaScript.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type = "text/javascript">
function GetSelectedRow(lnk) {
var row = lnk.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
var customerId = row.cells[0].innerHTML;
var city = row.cells[1].getElementsByTagName("input")[0].value;
alert("RowIndex: " + rowIndex + " CustomerId: " + customerId + " City:" + city);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns = "false" AllowPaging = "true" OnPageIndexChanging = "PageIndexChanging">
<Columns>
<asp:BoundField DataField = "CustomerID" HeaderText = "CustomerID" />
<asp:TemplateField HeaderText = "City">
<ItemTemplate>
<asp:TextBox ID="txtCity" runat="server" Text = '<%# Eval("City") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
to find the Index of row u can simply do this
GridViewRow gvr = e.Row;
or
int index = Convert.ToInt32(e.RowIndex);
and to find the value of the control for example text of the text box u can do this
Label lb_ID = (TextBox)GridInvoice.Rows[index].Cells[1].FindControl("Label_ID");
where index is row index and cell[1] is cell index ie if u have 4 cells in your grid then index starts from cell[0]... cell[3]. Here cell[1] means control is in cell 2 of ur grid . if u have grid values binding form database and you have some unique id like primary key coming also and u are performing opreration on this value in database then From Frond end add this property to gridview ( DatakeyNames="ur primary key or uniqe value comming form database "; and now if u have button or link button control ( or any other control which raises event in grid view then on the event write this code which will automatically capture primary key and row index for u.
protected void lnkbtnEdit_Click(object sender, EventArgs e)
{
GridViewRow gvrows = ((LinkButton)sender).NamingContainer as GridViewRow;
int32 Key = (Convert.ToInt32(grvCityList.DataKeys[gvrows.RowIndex].Values[0].ToString()));
Now do ur logic here
}
hope u will get what u are look for
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.