Thursday, May 19, 2011

Startup JavaScript Code from Content Page to Master Page

Let say, you have Master and Content pages and you want to add some JavaScript code from your Content pages that would run when the page is loaded. Content pages do not have the HTML elements like body tag to add your script on its onload event.

For this, you can use RegisterStartupScript method. Add the following code to your Content Page code behind Page_Load Event:

protected void Page_Load(object sender, EventArgs e)
{
Type type = GetType();
const string scriptName = "alertPopup";
if (!ClientScript.IsStartupScriptRegistered(type,scriptName))
{
ClientScript.RegisterStartupScript(type, scriptName, "alert('Hello World!')", true);
}

}
Happy Programming!!

No comments:

Post a Comment

Highly Appreciated your comments