Home  >  Article  >  Web Front-end  >  How to Trigger an ASP.NET Method from a JavaScript Click Event?

How to Trigger an ASP.NET Method from a JavaScript Click Event?

DDD
DDDOriginal
2024-11-11 20:17:02307browse

How to Trigger an ASP.NET Method from a JavaScript Click Event?

Trigger ASP.NET Method from JavaScript Click Event

When working on an ASP.NET web page, you may encounter the need to execute an ASP method from a JavaScript click event.

To achieve this without resorting to Ajax or other techniques, you can employ the following steps:

Integrating JavaScript Event with ASP

  1. Declare an Interface for the page class:

    public partial class Default : System.Web.UI.Page, IPostBackEventHandler
  2. Add the RaisePostBackEvent method to the code file:

    public void RaisePostBackEvent(string eventArgument) { }
  3. In the JavaScript onclick event, invoke the RaisePostBackEvent method:

    var pageId = '<%=  Page.ClientID %>';
    __doPostBack(pageId, argumentString);

This code initiates a postback to the ASP.NET page, allowing you to call the desired event and interact with the server-side logic from JavaScript.

The above is the detailed content of How to Trigger an ASP.NET Method from a JavaScript Click Event?. 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