1

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.

4 Answers 4

2

You can register it using:

Page.ClientScript.RegisterStartupScript()

For your case, it would be this:

Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "Start()", true);
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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()");
}

Comments

0

Try binding to the DOM document.ready event to load your content after the document has completed rendering

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.