Powered by Blogger.

Saturday, March 1, 2014

Body onload function in content page in asp.net:

In this post we will discuss how to use body onload function in content pages in asp.net.

Also you can check out my previous posts on:

- Confirm box example in JavaScript

- How to find the difference in retrieving data with and without using index in sql server 2008?

- Download Microsoft Enterprise Library 6 developer guide free

There are two approaches we can write body onload function in content pages.

1st approach:
<script type="text/javascript">
        document.body.onload = function () {alert('I am OK'); }

2nd Approach:
Modify the body tag by adding runat = "server" attribute like below:

<body id="body" runat="server">

Then in the content page in Page_Load write like below:

 protected void Page_Load(object sender, EventArgs e)
        HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("body");
        body.Attributes.Add("onLoad", "alert('I am also OK');");

Hope it will be helpful to you!