开发者

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

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


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.


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>


Please try this:

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


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


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>


try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜