Page.RegisterClientScriptBlock Method
Allows ASP.NET server controls to emit client-side script blocks in the Page.
[Visual Basic] Public Overridable Sub RegisterClientScriptBlock( _ ByVal key As String, _ ByVal script As String _ ) [C#] public virtual void RegisterClientScriptBlock( string key, string script ); [C++] public: virtual void RegisterClientScriptBlock( String* key, String* script ); [JScript] public function RegisterClientScriptBlock( key : String, script : String );
Parameters
- key
- Unique key that identifies a script block.
- script
- Content of script that is sent to the client.
Remarks
The client-side script is emitted just after the opening tag of the Page object's <form runat= server> element. The script block is emitted as the object that renders the output is defined, so you must include both tags of the <script> element.
By identifying the script with a 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 RegisterClientScriptBlock method in conjunction with the IsClientScriptBlockRegistered method. If the ECMAScript written in the code-declaration block has not already been registered, as tested by IsClientScriptBlockRegistered, the RegisterClientScriptBlock call is made.
[Visual Basic] <html> <head> <script language="VB" runat="server"> Sub Page_Load( sender as Object,e as EventArgs) 'Form the script that is to be registered at client side. Dim scriptString as String = "<script language=JavaScript> function DoClick() {" scriptString += "myForm.show.value='Welcome to Microsoft .NET'}<" scriptString += "/" scriptString += "script>" If(Not IsClientScriptBlockRegistered("clientScript")) RegisterClientScriptBlock("clientScript", scriptString) End If End Sub </script> </head> <body topmargin="20" leftmargin="10"> <form id="myForm" runat="server"> <input type="text" id="show" style="width=200"> <input type="button" value="ClickMe" onclick="DoClick()"> </form> </body> </html> [C#] <html> <head> <script language="C#" runat="server"> public void Page_Load(Object sender, EventArgs e) { // Form the script that is to be registered at client side. String scriptString = "<script language=JavaScript> function DoClick() {"; scriptString += "myForm.show.value='Welcome to Microsoft .NET'}<"; scriptString += "/"; scriptString += "script>"; if(!this.IsClientScriptBlockRegistered("clientScript")) this.RegisterClientScriptBlock("clientScript", scriptString); } </script> </head> <body topmargin="20" leftmargin="10"> <form id="myForm" runat="server"> <input type="text" id="show" style="width=200"> <input type="button" value="ClickMe" onclick="DoClick()"> </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
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family