0

Using asp.net, c# 3.5, vs 2008.

I have an aspx gridview called gv1 with a working c# codebehind onrowcommand method which is called when a button on the row is clicked.

The button on the gridview is being dynamically generated.

I am trying to replace that codebehind call with a javascript function to avoid a roundtrip to the server.

How do I do this?

So far I have tried

  1. adding a row attribute in the code behind when I am dynamically generating the button in a foreach loop , which didnt work:

    r.Attributes.Add("OnRowCommand", "javascript:gv1RowCommand(ID);");

  2. the following which gives an error before running: no overloaded method takes 1 arg

    <asp:GridView ID="gv1" runat="server" onrowcommand="gv1RowCommand(ID)" ...
    
  3. various other things such as event onchange , onselected

I am using the follow javascript just to see if I can get it to be called:

function gv1RowCommand(ID) { 
alert(
"row command"); 
}
Lill Lansey
  • 4,775
  • 13
  • 55
  • 77
  • I would suggest injecting javascript after the DOM is loaded, that way, if scripts are disabled, you can still use the postback to handle the even server-side. – John Gietzen Sep 25 '09 at 16:15
  • I can handle the event serverside already using protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer); etc... I am trying to avoid the postback. – Lill Lansey Sep 25 '09 at 16:20
  • You do not understand the Page life cycle. RowCommand is not a client-side event. – Josh Stodola Sep 25 '09 at 16:36
  • THank you for clearing that up for me. So that is what my question comes down to. I am looking for a client side equivalent of RowCommand, like ondblclick. Will this work: Dynamically generate an html button instead of an aspnet button in the gridview, and on the onClick event use parentElement.parentElement to get the row? – Lill Lansey Sep 25 '09 at 16:52

2 Answers2

2

You need to create object of your button first and then add attributes to it. check below code -

foreach (GridViewRow row in gv1.Rows)
 {
    //Creates object of button inside your gridView Row
    Button btnRowCommand = (Button)row.FindControl("YourButtonName");
    //Adds Attribute to button- in this case adds onclick attribute 
    //which will fire the 
    //gv1RowCommand(ID); javascript function
    btnRowCommand.Attributes.Add("onclick", "gv1RowCommand('"+ ID +"');"); 
 }

Instead of

// dont use this code  
 r.Attributes.Add("OnRowCommand", "javascript:gv1RowCommand(ID);");  
// dont use this code

This will cause your javascript function gv1RowCommand(ID) to fire when user clicks the button inside the gridview. If you need any more information of using javascript in gridView you can mail or reply. If the above code snippet is not enough for you, you can post much more code listing and will post the proper code accordingly.

naveen
  • 53,448
  • 46
  • 161
  • 251
Aamod
  • 52
  • 4
  • Also, you can add the click event to the row itself in the same way. That way the user only needs to click the row to fire the event (if that's the desired function). – Joel Etherton Jan 15 '10 at 16:14
0

Javascript events you can fiddle with:

Javascript Events

You don't have access to any of your page methods or properties in Javascript. You don't have access to anything persistant unless you're workng with Ajax... So there's not a lot that you'd do in RowCommand that you can usefully port to the client javascript layer.

quillbreaker
  • 6,119
  • 3
  • 29
  • 47