Compilation error on this string
-
05-07-2019 - |
Question
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"
Solution
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");
OTHER TIPS
"<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>"
@"<script language="Javascript" src="/utility/thickbox/thickbox-custom.js"></script>"