Javascript alert not working from code behind

后端 未结 3 1688
闹比i
闹比i 2021-01-28 11:29

I am generating a javascript alert box from codebehind in asp.net(vb).

The code:

            Catch ex As Exception
                MesgBox(\"Error in up         


        
3条回答
  •  -上瘾入骨i
    2021-01-28 11:53

    Add Imports System.Web.Script.Serialization to the top of your file, then try this:

    Private Sub MesgBox(ByVal sMessage As String)
        Dim serializer as New JavaScriptSerializer()
        Dim msgedtble As String = serializer.Serialize(sMessage)
        Page.ClientScript.RegisterStartupScript(Me.GetType, "myScripts",
            "")
    End Sub
    

    Using JavaScriptSerializer should take care of the linebreaks, single quotes, and everything else we haven't already thought of.

提交回复
热议问题