From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search

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

Personal tools

Served in 0.086 secs.