Home > Web Front-end > JS Tutorial > body text

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

DDD
Release: 2024-11-11 20:17:02
Original
306 people have browsed it

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
    Copy after login
  2. Add the RaisePostBackEvent method to the code file:

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

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template