Return to Snippet

Revision: 8042
at August 29, 2008 09:52 by Anthony


Initial Code
Private Sub RegisterJavascript()
    ' Define the name and type of the client scripts on the page.
    Dim ClientScriptName As String = "LinkClick"
    Dim PageType As Type = Me.GetType()

    ' Get a ClientScriptManager reference from the Page class.
    Dim csm As ClientScriptManager = Page.ClientScript

    ' Check to see if the client script is already registered.
    If (Not csm.IsClientScriptBlockRegistered(PageType, ClientScriptName)) Then
        Dim ClientScriptText As New StringBuilder()
        ClientScriptText.AppendLine("function ToAccountDetails(un) {")
        ClientScriptText.AppendLine("  document.getElementById(""UserName"").value = un;")
        ClientScriptText.AppendLine("  document.getElementById(""frmToAccountDetails"").submit();")
        ClientScriptText.AppendLine("}")

        csm.RegisterClientScriptBlock(PageType, ClientScriptName, ClientScriptText.ToString(), True)
    End If

End Sub

Initial URL

                                

Initial Description
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.

Initial Title
Programmatically Register Client-Side JavaScript

Initial Tags
javascript

Initial Language
VB.NET