Alligator tags (<%%>) inside js string?

I am trying to redirect a page reading url from a config file.

However, when I try to do this:

   <script type="text/javascript">
<%string redirectUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"];%>
    window.parent.location.replace("<%=redirectUrl%>");
</script>

      

alligator tags <%%> are not highlighted and when I run the following error appears on the yellow screen:

the controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

      

What am I doing wrong?

Thanks!

Edit:

This works if I just put the url right in the code like in

window.parent.location.replace("http://theurl.com");

      

but I need to change this depending on other things, so I need it to be in the config: S

+2


source share


6 answers


You will probably turn on the block inside

<head runat="server"> ... </head> 

      

block. If you want to use <%%> blocks, you need to remove the runat = "server" from the main tag, but then you will lose the Page.Title and some other functionality.



In your particular case, doing

window.parent.location.replace("<%= System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"] %>");

      

should fix the problem, i.e. get rid of the <%%> tags.

+3


source


I faced this problem several times. The problem is that ASP.NET does not know where to place some control it creates in the control hierarchy. I solved this problem by putting code in a server control like:



<asp:PlaceHolder runat="server">
    <script type="text/javascript">
        window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"]%>"); 
    </script>
</asp:PlaceHolder>

      

+1


source


Please try the following:

 <script type="text/javascript">
    window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"];%>");
</script>

      

0


source


   <script type="text/javascript"> 
    window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"]%>"); 
</script> 

      

0


source


protected void Page_Load(object sender,EventArgs e)
{
   Page.DataBind();
}

      

and use this

<script type="text/javascript">
   window.parent.location.replace('<%#System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"];%>');
</script>

      

0


source


try it

<script type="text/javascript">
<%= string redirectUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"] %>
window.parent.location.replace("<%=redirectUrl%>");
</script>

      

-1


source







All Articles