Articles → .NET → Call Javascript From Code Behind In Asp.Net
Call Javascript From Code Behind In Asp.Net
Software Requirement
Prerequisite Knowledge
- How to create a project using visual studio?
- Basic knowledge about basic controls like button etc and their events.
Steps Of Execution
- Create a new project
- Add controls to the page
- Add JavaScript code in code behind
Create A New Project
Click to Enlarge
Add Controls To The Page
Click to Enlarge
Add Javascript Code In Code Behind
using System;
public partial class _Default: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void Button1_Click(object sender, EventArgs e) {
this.ClientScript.RegisterStartupScript(this.GetType(), "ALERTMESSAGE", "alert('Button clicked')", true);
}
}
- Type – Type of start up script to register
- Key – This is the key of the script to register
- Script – JavaScript which you want to register.
- AddScripttags – It is a Boolean value (when set to true) ensures that script is added between the script tags.
Output
Click to Enlarge
Click to Enlarge