Click here to Skip to main content
15,885,985 members
Articles / Web Development / ASP.NET

JavaScript Registration in C# or VB.NET Code

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Dec 2007CPOL2 min read 33.9K   287   11  
Troubles putting JavaScript code inline with VB.NET or C# code
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class JSstring

    Dim vb As Boolean

    Private Sub JSstring_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        If txtFile.Text.Length = 0 Then
            MsgBox("Select a JavaScript file")
            Return
        End If

        vb = CBool(IIf(rbVb.Checked, True, False))

        Dim result As String
        If vb Then
            result = "Dim sb As StringBuilder = New StringBuilder()" + vbCrLf
            result += "sb.Append(""<script type=""""text/javascript"""" language=""""javascript"""">"")" + vbCrLf + vbCrLf
        Else
            result = "StringBuilder sb = new StringBuilder();" + vbCrLf
            result += "sb.Append(""<script type=\""text/javascript\"" language=\""javascript\"">"");" + vbCrLf + vbCrLf
        End If

        Dim strContents As String = ""
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader(txtFile.Text)
            strContents = objReader.ReadToEnd()
            objReader.Close()
        Catch Ex As Exception
            MessageBox.Show(Ex.Message)
        End Try

        Dim str() As String = Regex.Split(strContents.Trim, vbLf)
        Dim sb As StringBuilder = New StringBuilder()
        For i As Integer = 0 To str.Length - 1
            str(i) = str(i).Replace("""", """""")
            str(i) = str(i).TrimEnd(Chr(13))
            If vb Then
                sb.Append("sb.Append(""")
            Else
                sb.Append("sb.Append(@""")
            End If
            sb.Append(str(i))
            sb.Append(""")")
            If Not vb Then
                sb.Append(";")
            End If
            sb.Append(vbCrLf)
        Next

        result += sb.ToString
        result = result.TrimEnd(Chr(10))
        result += vbCrLf + vbCrLf
        result += "sb.Append(""</script>"")"
        If Not vb Then
            result += ";"
        End If
        result += vbCrLf + vbCrLf

        If vb Then
            result += "Dim js as String = sb.ToString"
            result += vbCrLf
            result += "ClientScript.RegisterStartupScript(Me.GetType(), ""JScript"", js)"
        Else
            result += "string js = sb.ToString();"
            result += vbCrLf
            result += "ClientScript.RegisterStartupScript(this.GetType(), ""JScript"", js);"
        End If

        txtResult.Text = result
    End Sub

    Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            txtFile.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub btnClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClipboard.Click
        Clipboard.Clear()
        If txtResult.Text.Trim.Length > 0 Then
            Clipboard.SetText(txtResult.Text)
        End If
    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Lifelong software developer. From IBM mainframes to Microsoft .Net, and lots in between.
Born and raised in Vilnius, Lithuania. Today live in Toronto, Ontario. Tomorrow - who knows?

Comments and Discussions