2

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.

Community
  • 1
  • 1
CptSupermrkt
  • 6,844
  • 12
  • 56
  • 87
  • Duh, I just realized that I could make ["onclick"] = "location.href='view.aspx?id=" + ... to pull off my querystring idea. Would still like to know what the story is with postback and calling a C# method. – CptSupermrkt Apr 13 '12 at 06:06

1 Answers1

2

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.

<script language="javascript" type="text/javascript">
    function PerformClick() {
        document.getElementById('<%=btn.ClientID %>').click();
    }
</script>

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>";

Pankaj
  • 9,749
  • 32
  • 139
  • 283