Start javascript from asp.net page
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 开发者_StackOverflow中文版a masterpage doesn't have a body tag.
You can register it using:
Page.ClientScript.RegisterStartupScript()
For your case, it would be this:
Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "Start()", true);
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.
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()");
}
Try binding to the DOM document.ready
event to load your content after the document has completed rendering
精彩评论