I’m using the code found here to make my gridview have clickable rows. The code for that is:
protected void gvdownloadaccounts_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; //hide the ID
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "Click to select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gvdownloadaccounts, "Select$" + e.Row.RowIndex);
}
}
…which works great!…except that I need to make it so that the “onclick” runs a C# method in the code behind. That method gets data from the database and fills in some web controls (like textboxes, etc.) with that data. This doesn’t seem like it should be that hard, so if someone could just give me a kick in the right direction, that’d be awesome.
I was toying with the idea of just re-directing to the same page, but with a query string, that way I could catch my code on page_load. But as one might expect:
e.Row.Attributes["onclick"] = Response.Redirect("www.google.com");
…doesn’t work.
Mark Up
<asp:Button ID="btn" runat="server" style="display:none;" OnClick="Btn_Click" OnClientClick="UpdateControl();"/>//This button will be hidden. It will be useful to perform a click and in code behind call the function in it’s handler. Now in the handler write the code to call your c# method.
Java Script
//Perforing the click of hidden button.
How will you call C# Method on each individual row click?
e.Row.Attributes["onclick"] = "<script language='javascript' type='text/javascript'>function PerformClick() {document.getElementById('<%=btn.ClientID %>').click();</script>";