devxlogo

Invoking a Client-side JavaScript Function from the Server-side Code

Invoking a Client-side JavaScript Function from the Server-side Code

.NET allows you to call a JavaScript function from the server-side code. For example, suppose you want to to display a message box to the user. This can be accomplished in two steps:

  1. Write a JavaScript function in the HTML view of the page inside the &ltHEAD&gt elements as shown below:
  2. In the code behind file of the .aspx page, add the following line of code in the page load function:
         private void Page_Load(object sender, System.EventArgs e)     {        Button1.Attributes.Add("OnClick", "ShowAlert();");     }

Now, compile the project and run it. Clicking the button on the page, you can see the message box displayed.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist