Programmatically Register Client-Side JavaScript

/ Published in: VB.NET
Save to your folder(s)

Gets a ClientScriptManager reference from the Page class. If the client script is not already registered on the page, builds and registers a script. Useful for building a script dynamically and ensuring that it gets registered during the correct stage of the page life cycle.

Copy this code and paste it in your HTML
  1. Private Sub RegisterJavascript()
  2. ' Define the name and type of the client scripts on the page.
  3. Dim ClientScriptName As String = "LinkClick"
  4. Dim PageType As Type = Me.GetType()
  6. ' Get a ClientScriptManager reference from the Page class.
  7. Dim csm As ClientScriptManager = Page.ClientScript
  9. ' Check to see if the client script is already registered.
  10. If (Not csm.IsClientScriptBlockRegistered(PageType, ClientScriptName)) Then
  11. Dim ClientScriptText As New StringBuilder()
  12. ClientScriptText.AppendLine("function ToAccountDetails(un) {")
  13. ClientScriptText.AppendLine(" document.getElementById(""UserName"").value = un;")
  14. ClientScriptText.AppendLine(" document.getElementById(""frmToAccountDetails"").submit();")
  15. ClientScriptText.AppendLine("}")
  17. csm.RegisterClientScriptBlock(PageType, ClientScriptName, ClientScriptText.ToString(), True)
  18. End If
  20. End Sub

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.