The Wight Stuff

Thursday, 22 March 2007

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


...

<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>

13 Comments:

Post a Comment



<< Home