Page.IsClientScriptBlockRegistered Method
Determines if the client script block is registered with the page.
[Visual Basic] Public Function IsClientScriptBlockRegistered( _ ByVal key As String _ ) As Boolean [C#] public bool IsClientScriptBlockRegistered( string key ); [C++] public: bool IsClientScriptBlockRegistered( String* key ); [JScript] public function IsClientScriptBlockRegistered( key : String ) : Boolean;
Parameters
- key
- The string key of the client script to search for.
Return Value
Returns true if the script block is registered; otherwise, false.
Remarks
Call this method before calling Page.RegisterClientScriptBlock to avoid unnecessarily assembling the client-side script. This is particularly important if the script requires a large amount of server resources to create.
Example
[Visual Basic, C#] The following example demonstrates the use of the RegisterClientScriptBlock method in conjunction with IsClientScriptBlockRegistered method. If the ECMAScript written in the code-declaration block has not already been registered, as tested by IsClientScriptBlockRegistered, the a 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
See Also
Page Class | Page Members | System.Web.UI Namespace | RegisterClientScriptBlock