Body OnLoad function in content page in ASP.NET

Posted: July 8, 2009 in ASP.NET
Tags:

Hi,

I wanted to certain script to run in the onload of the body for my content page.

To do this we need to do the following

Add an id and runat attribute to the body tag in the MASTER page

<body id=”body” runat=”server”>

And in the Content page’s  page load handler

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

Bye…

Comments
  1. oscar says:

    thanks… good!!!

  2. Anonymous says:

    Muchísimas Gracias!!!!!!!!!

  3. worldbijay says:

    I want to access my javascript throu an onload event of an asp.textbox control, is it possible

  4. Baris says:

    Thanks for the solution that I have been searching till the morning!!! :D You’r great!!!

  5. sir realy a good idea

  6. [...] the original here:  Body OnLoad function in content page in ASP.NET « Nishant Rana's … SHARETHIS.addEntry({ title: "Body OnLoad function in content page in ASP.NET « Nishant [...]

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s