Saturday 16 February, 2008

Page.RegisterStartupScript Method

Allows ASP.NET server controls to emit client-side script blocks in the Page.

[Visual Basic] Public Overridable Sub RegisterStartupScript( _    ByVal key As String, _    ByVal script As String _ ) [C#] public virtual void RegisterStartupScript(  string key,  string script ); [C++] public: virtual void RegisterStartupScript(  String* key,  String* script ); [JScript] public function RegisterStartupScript(    key : String,  script : String );

Parameters

key
Unique key that identifies a script block.
script
Content of script that will be sent to the client.

Remarks

Similar to the RegisterClientScriptBlock method, this method emits the script just before the closing tag of the Page object's <form runat= server> element. The script block is emitted as the object that renders the page is defined, so you must include both tags of the <script> element.

By identifying the script with the key, multiple server control instances can request the script block without it being emitted to the output stream twice.

Any script blocks with the same key parameter values are considered duplicates.

Note   Remember to include HTML comment tags around your script so that it will not be rendered if a requesting browser does not support scripts.

Example

[Visual Basic, C#] The following example demonstrates the use of the RegisterStartupScript method in conjunction with IsStartupScriptRegistered method. If the ECMAScript written in the code-declaration block has not already been registered, as tested by IsStartupScriptRegistered, the a RegisterStartupScriptBlock call is made.

[Visual Basic]  <html>   <head>      <script language="VB" runat="server">      public Sub Page_Load(Sender As Object,e As EventArgs)             ' Form the script to be registered at client side.          Dim scriptString As String  = "<script language=JavaScript> function DoClick() {"              scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft .NET!</h4>'}"          scriptString += "function Page_Load(){ showMessage1.innerHTML="          scriptString += "'<h4>RegisterStartupScript Example</h4>'}<"          scriptString += "/"          scriptString += "script>"               If(Not Me.IsStartupScriptRegistered("Startup")) Then         Me.RegisterStartupScript("Startup", scriptString)              End If     End Sub         </script>   </head>   <body topmargin="20" leftmargin="10" onload="Page_Load()">      <form id="myForm" runat="server">         <span id="showMessage1"></span>         <br>         <input type="button" value="ClickMe" onclick="DoClick()">         <br>         <span id="showMessage2"></span>      </form>   </body> </html>  [C#]  <html>   <head>     <script language="C#" runat="server">     public void Page_Load(Object sender, EventArgs e) {        // Form the script to be registered at client side.        String scriptString = "<script language=JavaScript> function DoClick() {";        scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft .NET!</h4>'}";        scriptString += "function Page_Load(){ showMessage1.innerHTML=";           scriptString += "'<h4>RegisterStartupScript Example</h4>'}<";        scriptString += "/";        scriptString += "script>";                     if(!this.IsStartupScriptRegistered("Startup"))          this.RegisterStartupScript("Startup", scriptString);      }        </script>   </head>   <body topmargin="20" leftmargin="10" onload="Page_Load()">      <form id="myForm" runat="server">         <span id="showMessage1"></span>         <br>         <input type="button" value="ClickMe" onclick="DoClick()">         <br>         <span id="showMessage2"></span>      </form>   </body> </html> 

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Hits4Pay