zoukankan      html  css  js  c++  java
  • ASP.NET2.0 在head中添加了代码块后出现的问题

    在ASP.MET2.0中的aspx页面文件中的head标签中添加一些东西可能造成一系列严重的问题,最近我们项目就是这个样子,幸好经过以下的测试大体知道一些原因所在,避免造成项目危险。
    如:
    一:
    后台代码:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class CommonQuery_PageRegStyle : System.Web.UI.Page
    {
        
    protected string strAlert = "Hello, Happy New Year!";
        
    protected void Page_Load(object sender, EventArgs e)
        
    {

        }

        
    protected override void OnPreRender(EventArgs e)
        
    {
            
            
    string cssFrageMent = "<style type='text/css'> "
                                   
    + ".CommonQueryBtn {"
                                   
    + "  border:0; 64px;height:20px;background:url('"
                                   
    + HttpRuntime.AppDomainAppVirtualPath + @"/images/BUTTON/btn.jpg');"
                                   
    +"     }</style>";
            LiteralControl include 
    = new LiteralControl(cssFrageMent);        
            
    this.Page.Header.Controls.Add(include);
            
    base.OnPreRender(e);
        }

    }


    前台代码:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageRegStyle.aspx.cs" Inherits="CommonQuery_PageRegStyle" %>

    <!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>
            
    <%=strAlert %>
        
    </title>
      
    </head>
    <body>
        
    <form id="form1" runat="server">
            
    <div>
                
    <asp:Button ID="Button1" runat="server" Text="Button" />
                
    <div>
                    
    <%=strAlert%>
                
    </div>
            
    </div>
        
    </form>
        
    <script type="text/javascript">
            
    function aa()
            
    {
                
    var a = '<%=strAlert %>';
                alert(a);
            }

            aa();
    </script>
    </body>
    </html>
    这样运行一切相安无事
    好改一下代码:
    修改标签<head></head>里的代码:
    <head runat="server">
        
    <title>
            
    <%=strAlert %>
        
    </title>
      
    <%=strAlert%>
    </head>
    运行结果出错了:
     

    “/ControlTest”应用程序中的服务器错误。

    控件包含代码块(即 <% ... %>),因此无法修改控件集合。

    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

    异常详细信息: System.Web.HttpException: 控件包含代码块(即 <% ... %>),因此无法修改控件集合。

    源错误:

    执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

    堆栈跟踪:

    [HttpException (0x80004005): 控件包含代码块(即 <% ... %>),因此无法修改控件集合。]
                System.Web.UI.ControlCollection.Add(Control child) +233
                System.Web.UI.PageTheme.SetStyleSheet() +365
                System.Web.UI.Page.OnInit(EventArgs e) +32
                System.Web.UI.Control.InitRecursive(Control namingContainer) +345
                System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1795
                
    好再修改一下代码:
    <head runat="server">
        
    <title>
            
    <%=strAlert %>
        
    </title>
      
    <script type="text/javascript">
            
    function aa()
            
    {
                
    var a = '<%=strAlert %>';
                alert(a);
            }

            aa();
        
    </script>
    </head>

    一样的错误再次出现。得出的结果:在动态为<head></head>添加代码时,<head></head>标签中非<title>标签不能包含<%...%>这样的代码块。
    二:
    好现在我们重新新建一个文件
    后台代码:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class CommonQuery_TestStylePage : System.Web.UI.Page
    {
        
    protected string strAlert = "Hello, Happy New Year!";
        
    protected void Page_Load(object sender, EventArgs e)
        
    {

        }

    }
    这次没有在head中动态添加东西
    前台:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestStylePage.aspx.cs" Inherits="CommonQuery_TestStylePage" %>

    <!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><%=strAlert %></title>
        
    <%=strAlert %>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
        
        
    </div>
        
    </form>
    </body>
    </html>

    以上代码运行没有问题
    但 ......
    再添加一东西后... 如图,添加了主题,主题里包含了一些Css样式文件

    再运行测试,结果又是出现以上的错误。。郁闷啊。。

    有知有何好的解决方法呢?

    综上所试,本人觉得开发ASP.NET或网面时,最好的方法就是将一些JavaScript代码控制页面的一些东西写在页面下端即</body>之上。

    一些样式相关的控制外观的东西最好就写在<head></head>中以避免出现一些不可预见的问题,当然这样做也是最符合各自职能执行的顺序

    转载请注明出处[http://samlin.cnblogs.com/

    欢迎关注本人公众号:

    作者赞赏
  • 相关阅读:
    How to function call using 'this' inside forEach loop
    jquery.validate.unobtrusive not working with dynamic injected elements
    Difference between jQuery.extend and jQuery.fn.extend?
    Methods, Computed, and Watchers in Vue.js
    Caution using watchers for objects in Vue
    How to Watch Deep Data Structures in Vue (Arrays and Objects)
    Page: DOMContentLoaded, load, beforeunload, unload
    linux bridge
    linux bridge
    EVE-NG网卡桥接
  • 原文地址:https://www.cnblogs.com/samlin/p/1026321.html
Copyright © 2011-2022 走看看