Tuesday, August 08, 2006

Body onLoad() event in content pages - ASP.net 2.0

How we can use the onLoad() event in ASP.Net 2.0 content pages ?

Simple !.
A content page is basicall a page inherited from a MasterPage (.master) in .net framework 2.0. This can be adhieved by either two ways.

1. Javascript function override

Add body onLoad() event in the master page and override the function used in the event in the inherited content pages.

Say,

in master page
<html>
<head>
<script language=" javascript" type=" text/javascript">
function testonload(){ //an empty function
var x = 'empty';
}
</script>
</head>


.
..
...
....
.....



in content page

<asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1">
<script language="javascript" type="text/javascript">
function testonload(prodid, div) {
//client script logic goes here..!!
}
</script>


2. Bind client script to body in code behind

Give an id to the body tag of the masgter page and add runat="server" sot that it is available in the code behind.

Now in content pages, add the following code in the page_load event


HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("bodyMaster");
body.Attributes.Add("onload", "toggle('parameters');");


Keep coding,



3 comments:

SharePoint Charm said...

First option doesn't look right. How is the testonload() called?

dotNetSoldier said...

You can bind that event in the body tag of master page. and that itself can be an empty implemenation. In the content pages, you can implement the logic (override)....This does works..

Unknown said...

Wats up Soldier
Did the override method, works perfectly .. thanx!