内容包括一句:  var a="<%=Request.ApplicationPath  %>";
放在asp.net的<header>里就会报错,放在<body>里不报错。而如果去掉这一句就放在哪里都不报错。
我的问题是 
1.asp.net里的javascript放在哪里比较好。
2.为什么在<header>里要求不能有<% ... %>)?请从asp.net的页面生成的角度来解释这个要求的必然性。
Server Error in '/WebApp' Application.
--------------------------------------------------------------------------------The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  Stack Trace: 
[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
   System.Web.UI.ControlCollection.AddAt(Int32 index, Control child) +2105879
   System.Web.UI.PageTheme.SetStyleSheet() +345
   System.Web.UI.Page.OnInit(EventArgs e) +2057570
   System.Web.UI.Control.InitRecursive(Control namingContainer) +321
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +692 

解决方案 »

  1.   

    1、放在哪里都可以,看需要了。
    2、不是不能有<%%>,关键是看其中是什么内容了.
      

  2.   

    asp.net里的javascript放在哪里比较好。
    ------------------------------------
    静态的就放在<head></head>里吧,动态的就用scriptmanage类动态生成注册.
    2.为什么在 <header> 里要求不能有 <%   ...   %> )?请从asp.net的页面生成的角度来解释这个要求的必然性。 
    var   a=" <%=Request.ApplicationPath     %> "; 
    Request.ApplicationPath???明明是服务端的语言啊.
      

  3.   

    行啊
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
        document.title="<%=Request.ApplicationPath %>";
        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
      

  4.   

    1.asp.net里的javascript放在哪里比较好。
    -------------------------
    我认为放在最靠近</body>最好,这样的避免对象未定义错误,不过W3C规范是要放在head里.
    2.为什么在 <header> 里要求不能有 <%   ...   %> )?请从asp.net的页面生成的角度来解释这个要求的必然性。 
    ------------------------
    可以,你可能哪里搞错了.另外,W3C标准,script是不必要language属性的.
      

  5.   

    第一个问题是乎没什么意义,无论放在那里,好像区别都不大
    第二个问题 <%   ...   %> asp.net 中不提倡用吧,这个好像是从ASP过度来了
      

  6.   

    那可能是因为我的这个是master page的关系,实际上述代码是放在一个site1.master中的。
    确实是有报错的。