views:

106

answers:

3

I'm trying to create the following string:

<script runat="server" type="text/C#">
    protected void Page_Load(object sender, EventArgs e)
    {
        Parent.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "<script type='text/javascript' langauage='javascript' src='test.js'></script>");
    }
</script>

yet i get a compilation error in VS saying "Newline in constant"

A: 
@"<script language="Javascript" src="/utility/thickbox/thickbox-custom.js"></script>"
sylvanaar
+1  A: 

Your problem is the end script tag

http://support.microsoft.com/kb/827420

Solve it wit:

".....<"+"/SCRIPT>"

or maybe

".....<\/script>"

Use ClientScriptManager instead and use RegisterClientScriptInclude. This way you only need to have the file name in a string.

ClientScriptManager.RegisterClientScriptInclude

ClientScriptManager scriptManager = new ClientScriptManager(); scriptManager.RegisterClientScriptInclude("filename.js");
skyfoot
I like your alternative solution by using RegisterClientScriptInclude. =)
burnt1ce
+2  A: 
"<script type='text/javascript' langauage='javascript' src='test.js'></script>"

Well yeah, you've got string containing </script> inside a <script> element. That closes the outer <script>, so the code that appears to be inside your runat-server script is just:

protected void Page_Load(object sender, EventArgs e)
{
    Parent.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "<script type='text/javascript' langauage='javascript' src='test.js'>

And as the error says, that contains a "string with no terminating double-quote.

Try escaping the characters so the other script block doesn't see them as markup:

"\x3Cscript type='text/javascript' src='test.js'>\x3C/script>"
bobince