views:

197

answers:

3

Hi,

I am creating an ASP.NET custom control.

I want to set the body onload event of the ASPX page where the control resides.

Please note I cannot rely on the body tag in the ASPX page having runat="server".

any ideas??

Cheers.

+2  A: 

Inline javascript! Just incase you can't use jQuery

 function addLoadEvent(func) {   
   var oldonload = window.onload;   
   if    (typeof window.onload != 'function')
      {
        window.onload = func;   
      } else {
     window.onload = function() {
       if (oldonload) {
         oldonload();
           }
       func();
     }   } }

 addLoadEvent(initialize);

link to read http://www.webreference.com/programming/javascript/onloads/
Credits goto http://simonwillison.net/

ggonsalv
+2  A: 

If you include jQuery in your project you can use this:

jQuery(document).ready(function () 
{
    // page load code here
});

You can run these multiple times on a page, ie handy within a control

Mark Redman
+1  A: 

Try this,

<script type="text/javascript">
 window.onload  = initialize();

   function initialize() {
          //your stuff
   }
 </script>
Pandiya Chendur
That is intrusive since it will break any other onlonad events since it is a control.
ggonsalv
@ggonsalv i used this in a masterpage... Ya you are right..
Pandiya Chendur