RegisterClientScriptBlock and RegisterStartupScript in ASP.Net - SQL Programmers

RegisterClientScriptBlock and RegisterStartupScript in ASP.Net

09/20/2010

RegisterClientScriptBlock and RegisterStartupScript both used to register and execute JavaScript code after the page finished loading, or any submit operation is performed by the user.

In a page where the RegisterStartupScript method is used, the JavaScript code will be placed at the bottom of the ASP.Net page, just before the closing form element (). If the RegisterClientScriptBlock method is used, the JavaScript code will be placed at the top of the ASP.Net page just after the opening form element (

). The RegisterStartupScript method will render the script after all elements in the web page.

 

Syntax

RegisterClientScriptBlock(type,key,script)

Here, type is System.Type, key is a String value that uniquely identifies the script block, and script is the script block.

The RegisterStartupScript method has a similar syntax to the RegisterClientScriptBlock method.

RegisterstartupScript(type,key,script)

RegisterClientScriptBlock Example

Call MessageBox("Test RegisterClientScriptBlock Message")

Sub MessageBox(ByVal strMsg As String)
     Dim StrMessage As String
     StrMessage = ""
     ClientScript.RegisterClientScriptBlock(Me.GetType, "MessageBox", StrMessage)
End Sub

Here

  • MessageBox is the unique identifier for the script block.
  • StrMessage contains the script block.

RegisterStartupScript Example

Call MessageBox("Test RegisterStartupScript Message")

Sub MessageBox(ByVal strMsg As String)
     Dim StrMessage As String
     StrMessage = ""
     ClientScript.RegisterStartupScript(Me.GetType, " MessageBox1", StrMessage)
End Sub

The above examples display an alert message when it is called.

IsStartupScriptRegistered() and IsClientScriptBlockRegistered()

These two are the two helper methods used when emitting the client script. These two methods return the Boolean value, by checking the registration  of the script block.

Syntax

IsStartupScriptRegistered(key)
IsClientScriptBlockRegistered(key)

Here key is a String value that uniquely identifies the script block

RegisterClientScriptBlock while using AJAX Update Panel

If an Update panel is used in the page then the RegisterClientScriptBlock method is registered using ScriptManager control.

Syntax

RegisterClientScriptBlock(control,type,key,script,addScriptTags)

Here,

  • control represents the control that is registering the client script block.
  • type is the type of the client script block.
  • key is a String value that uniquely identifies the script block.
  • script is the script block.
  • addScriptTags is the Boolean value which is true to enclose the script block in tags.

Example

Dim StrMessage As String
StrMessage = ""
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), StrMessage, False)

Here addScriptTags is False, so we enclosed the script block by script tags.

Dim StrMessage As String
StrMessage = "alert('Test Message')"
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), StrMessage, True)

Here addScriptTags is set to True, so we don’t need to explicitly enclose the script block with script tags.