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 ' />
5
  • Use Ajax call to the method Commented Sep 30, 2015 at 16:30
  • Try this answer about PageMethods , it for sure will help you Commented Sep 30, 2015 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) Commented Sep 30, 2015 at 16:34
  • @EnriqueZavaleta: That might help. Let me do my research from that angle... Commented Sep 30, 2015 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... Commented Sep 30, 2015 at 18:00

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.