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
Declare an Interface for the page class:
public partial class Default : System.Web.UI.Page, IPostBackEventHandler
Add the RaisePostBackEvent method to the code file:
public void RaisePostBackEvent(string eventArgument) { }
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!