Powered by Blogger.

Sunday, February 16, 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:

- Date validation using JavaScript

- Write exception to Event Log in Asp.Net C#.Net

- C#.Net basic interview questions and answers

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!