views:

67

answers:

4

Hi I have a usercontrol which includes some JavaScript, if I add the control to a standard web page I can start the JavaScript in the body tag, like this

<body onLoad="Start()">

The problem is that I need to add the control to a webpage which is inside a masterpage, how do I then start the script when a page inside a masterpage doesn't have a body tag.

A: 

load it on the document.ready

Chino
+2  A: 

You can register it using:

Page.ClientScript.RegisterStartupScript()

For your case, it would be this:

Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "Start()", true);
Nick Craver
+2  A: 

Include jQuery and put your initialization code inside $(document).ready().

If you want to strictly use .NET functionality, and your page is properly marked up with runat="server", you could use Page.ClientScript.RegisterStartupScript() as well.

Justin Niessner
+1  A: 

You can change the body tag to runat server and give it an id.

<body id="masterBody" runat="server">

Then on page load:

public void Page_Load(Object sender, EventArgs e)
{ 
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("masterBody");
body.Attributes.Add("onload", "Start()");
}
curtisk