All-Star
18815 Points
3831 Posts
Jun 15, 2016 07:19 AM|Nan Yu|LINK
Hi ,
If you want to click row in gridview and then call sever side function , for example, change the color of selected row , below code is for your reference :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication3.test" EnableEventValidation = "false"%> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.10.2.js"></script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView> <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton> </form> </body> </html>
Code behind :
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") }); dt.Rows.Add(1, "John Hammond", "United States"); dt.Rows.Add(2, "Mudassar Khan", "India"); dt.Rows.Add(3, "Suzanne Mathews", "France"); dt.Rows.Add(4, "Robert Schidner", "Russia"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex); e.Row.ToolTip = "Click to select this row."; } } protected void OnSelectedIndexChanged(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { if (row.RowIndex == GridView1.SelectedIndex) { row.BackColor = ColorTranslator.FromHtml("#A1DCF2"); row.ToolTip = string.Empty; } else { row.BackColor = ColorTranslator.FromHtml("#FFFFFF"); row.ToolTip = "Click to select this row."; } } }
You could also use jquery to find selected row : http://www.aspsnippets.com/Articles/Change-Gridview-Row-Color-OnClick-without-PostBack-in-ASPNet.aspx
Best Regards,
Nan Yu
All-Star
18815 Points
3831 Posts
Re: call function on grid view row
Jun 15, 2016 07:19 AM|Nan Yu|LINK
Hi ,
If you want to click row in gridview and then call sever side function , for example, change the color of selected row , below code is for your reference :
Code behind :
You could also use jquery to find selected row :
http://www.aspsnippets.com/Articles/Change-Gridview-Row-Color-OnClick-without-PostBack-in-ASPNet.aspx
Best Regards,
Nan Yu