简体   繁体   中英

ClientScript doesn't work in codebehind

I created the script to call the javascript in code behind. However it has an error. I didn't figure out what is wrong. Would someone point it out to me. Thanks.

There is my code:

Dim strScript As String = "<script language=javascript>alert('test/page1.aspx?loctionFolder=
                      Server.HtmlEncode(hdFolderLocation.Value) & "');return false; </script>"

            If Not ClientScript.IsStartupScriptRegistered(Me.GetType(), "download") Then
                Page.ClientScript.RegisterStartupScript(Me.GetType(), "download", strScript, True)
            End If

You missed double quote and concatenation before Server.HtmlEncode(hdFolderLocation.Value)

Dim strScript As String = "<script language=javascript>alert('test/page1.aspx?loctionFolder=" & 
                      Server.HtmlEncode(hdFolderLocation.Value) & "');return false; </script>"

        If Not ClientScript.IsStartupScriptRegistered(Me.GetType(), "download") Then
            Page.ClientScript.RegisterStartupScript(Me.GetType(), "download", strScript, True)
        End If

RegisterStartupScript Method 's last parameter is addScriptTags .

In your case, you set it as strScript, True) , so you do not need script tag .

Dim strScript As String = "alert('test/page1.aspx?loctionFolder=" & 
   Server.HtmlEncode(hdFolderLocation.Value) & "');return false;";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM