RegisterStartupScript
This article is an orphan, as few or no other articles link to it. Please introduce links to this page from related articles; suggestions are available. (February 2009) |
The introduction to this article provides insufficient context for those unfamiliar with the subject. Please help improve the article with a good introductory style. (October 2009) |
In ASP.NET, the RegisterStartupScript method is used to render JavaScript to a web browser.
This method adds the code into the OnPreRender method, rather than into the Render method. This method allows every control to inform the page about the client-side script it needs to render. After the Render method is called, the page is able to render all the client-side script it collected during the OnPreRender method. If the client-side script registration methods in the Render method are called, the page has already completed a portion of its rendering before the client-side script can render itself.
- Sample (C#)
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterStartupScript( typeof(Page), “ControlFocus”, ”document.getElementById(‘“ + this.ClientID + “‘).focus();”, true);
}
In this listing, the code emits client-side script to automatically move the control focus to the TextBox control when the Web page loads. When you use the RegisterStartupScript method, notice that it now includes an overload that specifies if the method should render surrounding script tags. This is necessary if rendering more than one client script to the page.
The method requires a key parameter, which is used to uniquely identify the script block; if more than one script block is being registered in the Web page, each block is supplied a unique key. The IsStartupScriptRegistered method can be used to determine if a particular script block has been previously registered on the client using the RegisterStartupScript method.
External links
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...
- Pages using deprecated source tags
- Pages with syntax highlighting errors
- Pages with broken file links
- Orphaned articles from February 2009
- Articles with invalid date parameter in template
- All orphaned articles
- Wikipedia articles needing context from October 2009
- All Wikipedia articles needing context
- Wikipedia introduction cleanup from October 2009
- ASP.NET