Thursday, September 15, 2011

Body onload function in content page in asp.net

Body onload function in content page in asp.net

While working on one of the application, I had a master page & on one of the content page's body onload, I had to run a JS function.

To do this first you need to add an ID & runat attribute to the master page's body control. Like this:

In master page:

--code <body id="body" runat="server"> </body>

Now in content page, in the page load event, add this code:

protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl body = this.Master.FindControl("body") as HtmlGenericControl;
body.Attributes.Add("onLoad", "alert('Hello World');");
}

I hope this simple tip will be helpful...

All Rights Reserved DotNetAnna | Blogger Template by Bloggermint