Home >Backend Development >C++ >Can I Call ASP.NET Functions from JavaScript Click Events Without Ajax?

Can I Call ASP.NET Functions from JavaScript Click Events Without Ajax?

Susan Sarandon
Susan SarandonOriginal
2025-01-25 10:52:09806browse

Can I Call ASP.NET Functions from JavaScript Click Events Without Ajax?

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:

  1. 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>
  2. This will add the RaisePostBackEvent method to your code file:

    <code class="language-csharp"> public void RaisePostBackEvent(string eventArgument) { }</code>
  3. 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:

  • Use double underscores __doPostBack with no spaces between the underscores.
  • The P.S. in the original answer refers to Markdown syntax and has nothing to do with this discussion.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn