1

Sorry if this is a simple answer, but I can't seem to find this (on SO or Google.)

I have an aspx page, with a GridView and each record has a button.

What I need to do is execute a bit of Javascript on each record that requests a bit of data from the underlying page class. The method on the class is static, so I'm not interested in the instance.

Also, I want to call this method WITHOUT doing a postback. Can this be done and, if so, how do I do this?

NOTE

I know that everyone always wants the code but, essentially, I'm looking for a snippet that I can put within an OnClientClick event. Something like this:

<asp:Button id="myButton" 
    runat="server" 
    ... 
    OnClientClick='PageClass.UserMustConfirm()?confirm("Are you sure you want to continue?") : true ' />
RLH
  • 15,230
  • 22
  • 98
  • 182
  • Use Ajax call to the method –  Sep 30 '15 at 16:30
  • Try [this answer about PageMethods](http://stackoverflow.com/a/30083658/4773983) , it for sure will help you – Enrique Zavaleta Sep 30 '15 at 16:34
  • I'm sorry, I'm very new to web development (been a winforms/wcf developer for over 10 years.) I certainly know what "ajax" means, but I'm not sure how to properly set all that up from a traditional aspx, webforms object. Also, it is worth mentioning that this call is taking place in a UserControl. I need to call the method on the UserControl. Sorry for any confusion (especially on my part. :P) – RLH Sep 30 '15 at 16:34
  • @EnriqueZavaleta: That might help. Let me do my research from that angle... – RLH Sep 30 '15 at 16:35
  • If your sample code is describing your real need and is not just a simplified sample, it seems like you need to call the server side method in order know if the confirm should be shown or not. If so, maybe you could move the logic to (for instance) the `OnRowDataBound` event? Then you would not need to call the server side method at all... – user1429080 Sep 30 '15 at 18:00

1 Answers1

1

Like others in the comments have already said, you're going to need to use Ajax or PageMethods...but I wanted to clear up a few misconceptions you may have about how this works in the first place.

There is a clear separation from code that runs in the client vs code that runs in the server -- this is no way for you to call server code directly from client code -- how could there be? Instead, you need to make a request to the server where that code "lives", get the response, and do something meaningful with it in the client -- that is what PageMethods does: it opens up a method as a WebMethod and lets the client code do something seamlessly.

But you shouldn't do that.

Instead of relying on what is basically an ancient framework (WebForms), look into using jQuery's $.ajax(...) and Promise objects to do this for you in a more explicit, framework-agnostic way.

Short answer: you cannot directly call C# from JavaScript that runs (in the browser at least).

Long answer: you can use Ajax to send the server parameters to call C# code with and do something with the returned result.

Hope that helps!

funbrigade
  • 321
  • 2
  • 7