RegisterStartupScript

RegisterStartupScript

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

* [http://msdn2.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx Page.RegisterStartupScript Method ]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”