Using body onload with ASP.net 2.0 MasterPages
I was recently scratching my head trying to figure out how to use Master Pages with .net 2.0 and Visual Web Developer. There didn't seem to be an out-of-the-box way to do this, so this is how I did it.
MasterPage.master
This way, if i have a content page that may require an onload event then I just create a function called body_onload in Headers content area of each page that requires it.
Default.aspx
MasterPage.master
...
<head>
<asp:ContentPlaceHolder runat="server" id="Headers">
</asp:ContentPlaceHolder>
<script language=javascript>
function mp_onload()
{
if(window.body_onload != null)
window.body_onload();
}
</script>
</head>
<body onload="mp_onload();">
...
This way, if i have a content page that may require an onload event then I just create a function called body_onload in Headers content area of each page that requires it.
Default.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="Headers" Runat="Server">
<script language="javascript">
function body_onload()
{
//do something
}
</script>
</asp:Content>

Subscribe to
13 Comments:
Hai, Thanks, I was trying almost all those methods found in the Net and found yours the most easy and working as required
Saleem
By
Saleem, At
16 September 2007 20:29
Did the trick for me too!
Thumbs up!
Tom
By
Anonymous, At
22 October 2007 15:17
exactly what I've been trying to find out all afternoon.. thanks!
By
swordfishBob, At
19 November 2007 06:08
Thanks - used this construct for the body unload event too. Could not find any other way of firing a body unload event on a slave page.
By
Donal, At
19 November 2007 16:09
Thank u very much ;) a good trick.
thanks my friend!!
By
Ana, At
09 January 2008 22:16
Work perfect. Thanks just what I needed.
By
Anonymous, At
12 March 2008 05:49
I've been trying to do this for an hour. Nice Trick!!!...Thanks
By
Anonymous, At
28 March 2008 23:12
npsgxqwWonderful, easy to understand and worked beautifully.
By
Anonymous, At
08 April 2008 17:43
wow thx hello from mexico city
By
Anonymous, At
17 April 2008 18:34
I'm new to javascript
Sorry but it's not working for me
I'm using a alert in both master and content page. However the alert in master page is working
while that in content page is not working.
i'm not too sure how to call body_onload in the content page.
Hopefully one can find a solution .
By
Bikram, At
24 April 2008 16:41
genius! thank you, that worked wonderfully.
By
Troy, At
09 July 2008 14:46
wonderfull solution!!!
By
Anonymous, At
07 September 2008 03:06
WoW!
so simple and great,
u r a genius!
By
Anonymous, At
30 September 2008 21:14
Post a Comment
<< Home