Call ASP.NET functions from JavaScript click events without using Ajax
Scenario:
You have an ASP.NET web page that contains JavaScript code and a submit button with a click event. You're wondering if it's possible to call an ASP.NET method from a JavaScript click event.
Answer:
Yes, it is possible to call ASP.NET methods from a JavaScript click event without using Ajax or other technologies. Here’s how:
Steps:
In your code file, implement the IPostBackEventHandler interface in your Page class:
<code class="language-csharp"> public partial class Default : System.Web.UI.Page, IPostBackEventHandler</code>
This will add the RaisePostBackEvent method to your code file:
<code class="language-csharp"> public void RaisePostBackEvent(string eventArgument) { }</code>
In the JavaScript click event handler, use the following code:
<code class="language-javascript"> var pageId = ''; __doPostBack(pageId, argumentString);</code>
This will call the RaisePostBackEvent method passing the arguments specified in argumentString as event parameters. From here, you can call any other events you want.
Note:
__doPostBack
with no spaces between the underscores. The above is the detailed content of Can I Call ASP.NET Functions from JavaScript Click Events Without Ajax?. For more information, please follow other related articles on the PHP Chinese website!