views:

170

answers:

6

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

However, when I try this:

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

the alligator tags <% %> are Not being highlighted, and when I run I get the following error in the yellow screen:

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

What am I doing wrong??

Thanks!

Edit:

It does work if I just put the url straight into the code, as 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

A: 

try this

<script type="text/javascript">
<%= string redirectUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"] %>
window.parent.location.replace("<%=redirectUrl%>");
</script>
Nnp
this way the url will be written to the page (<%= means Response.Write) and I dont think this will work either.
Raja
A: 

Please try this:

 <script type="text/javascript">
    window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"];%>");
</script>
Raja
A: 
   <script type="text/javascript"> 
    window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"]%>"); 
</script> 
DaveB
No that doesnt work either
Francisco Noriega
+3  A: 

You're probably including the block inside a

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

block. If you want to use <% %> blocks you need to remove the runat="server" from the head tag but then you'll lose the Page.Title and some other features.

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.

pbz
yup, it was in the <head> tags... moved to the body and now its working :) thanks!
Francisco Noriega
+1  A: 

I've faced this issue several times. The problem is that ASP.NET does not know where to place some control it creates in the control hierarchy. I've solved this issue by placing code in the server control, e.g.:

<asp:PlaceHolder runat="server">
    <script type="text/javascript">
        window.parent.location.replace("<%=System.Web.Configuration.WebConfigurationManager.AppSettings["RedirectURL"]%>"); 
    </script>
</asp:PlaceHolder>
Andrew Bezzub
+1: This is a better solution than the accepted answer when moving the insertion point is not an option. It also doesn't require you to remove runat="server" from the head tag.
Joel Mueller
A: 
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>
Braveyard