关于c#:“控件集合无法修改,因为控件包含代码块”

“The Controls collection cannot be modified because the control contains code blocks”

我正在尝试创建一个简单的用户控件,它是一个滑块。当我将Ajaxtoolkit SliderXTender添加到用户控件时,我会得到这个(&;$35;()@错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    Server Error in '/' 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.Add(Control child) +8677431    AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) in d:\E\AjaxTk-AjaxControlToolkit
elease\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:293 AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) in d:\E\AjaxTk-AjaxControlToolkit
elease\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:306 System.Web.UI.Control.LoadRecursive()
    +50    System.Web.UI.Control.LoadRecursive()
    +141    System.Web.UI.Control.LoadRecursive()
    +141    System.Web.UI.Control.LoadRecursive()
    +141    System.Web.UI.Control.LoadRecursive()            
    +141    System.Web.UI.Control.LoadRecursive()
    +141    System.Web.UI.Control.LoadRecursive()
    +141    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


    Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074

我已经尝试在用户控件中放置一个占位符,并以编程方式将文本框和滑块扩展程序添加到占位符中,但仍然收到错误。

下面是简单的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<table cellpadding="0" cellspacing="0" style="width:100%">
    <tbody>
        <tr>
            <td></td>
            <td>
               
               
            </td>
        </tr>
        <tr>
            <td style="width:60%;">
                :&nbsp;&nbsp;
            </td>
            <td style="text-align:right;width:40%;">                

                   
                    <ajaxToolkit:SliderExtender ID="seSlider" runat="server"
                        BehaviorID="seSlider"
                        TargetControlID="txtSlider"
                        BoundControlID="lblSliderValue"
                        Orientation="Horizontal"
                        EnableHandleAnimation="true"
                        Length="200"
                        Minimum="0"
                        Maximum="100"
                        Steps="1" />

            </td>
        </tr>
    </tbody>
</table>

有什么问题?


首先,用<%而不是<%=开始代码块:

1
2
3
4
5
<head id="head1" runat="server">
  My Page
  <link href="css/common.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="<%# ResolveUrl("~/javascript/leesUtils.js") %>">
</head>

这会将代码块从响应更改为数据绑定表达式。由于<%# ... %>数据绑定表达式不是代码块,因此clr不会抱怨。然后在母版页的代码中添加以下内容:

1
2
3
4
protected void Page_Load(object sender, EventArgs e)
{
  Page.Header.DataBind();    
}


我也遇到了这个问题,但找到了另一个解决办法。

我发现用asp:placeholder标记包装代码块可以解决这个问题。

1
2
  <meta name="ROBOTS" content="<%= this.ViewData["RobotsMeta"] %>" />
</asp:PlaceHolder>

(我使用的CMS是从一些代码插入头部分,在代码后面限制我添加带有各种信息(如元标记等)的自定义控制块,因此这是唯一适合我的方法。)


我可以确认,将带有<% %>标记的javascript从head移到form标记可以修复此错误。

http://italez.wordpress.com/2010/06/22/ajaxcontroltoolkit-calendarexter-e-strana-eccezione/


将javascript放在一个DIV标记下。

1
2
 //div tag must have runat server
  //Your Jave script code goes here....

会起作用的!!


如果在页面中使用脚本管理器,则可以执行相同的功能。你必须像这样注册脚本

1
2
3
4
<Scripts>
     
</Scripts>
</asp:ScriptManager>


我在用户控件中有同样的问题。托管控件的页面在头标记中有注释,我删除了这些注释,之后一切都正常。一些帖子还建议从头部删除脚本,并将它们放在身体中。


我尝试使用<%# %>,但没有成功。然后我把代码中的Page.Header.DataBind();改为this.Header.DataBind();,它运行得很好。


在我的例子中,我已经用<%# %>替换了<%= %>,它起作用了!


我有这个问题,但不是通过标题。我的占位符在正文中。所以我把所有的<%=都换成了<%#,然后

1
2
3
4
protected void Page_Load(object sender, EventArgs e)
{
    Page.Header.DataBind();    
}

它奏效了。


将Java脚本代码保存在体标签中

1
2
3
4
<body>
   <script type="text/javascript">
   
</body>

"<%"数据绑定技术不会直接在标记中的标记内工作:

1
2
3
4
5
6
<head runat="server">

  <link rel="stylesheet" type="text/css"
        href="css/style.css?v=<%# My.Constants.CSS_VERSION %>" />

</head>

上述代码的计算结果为

1
2
3
4
5
6
<head>

  <link rel="stylesheet" type="text/css"
        href="css/style.css?v=&lt;%# My.Constants.CSS_VERSION %>" />

</head>

相反,您应该执行以下操作(注意里面的两个双引号):

1
2
3
4
5
6
<head runat="server">

  <link rel="stylesheet" type="text/css"
        href="css/style.css?v=<%#"" + My.Constants.CSS_VERSION %>" />

</head>

你会得到想要的结果:

1
2
3
4
5
<head>

  <link rel="stylesheet" type="text/css" href="css/style.css?v=1.5" />

</head>

我也面临同样的问题。我找到了如下解决方案。

解决方案1:我把我的脚本标签放在尸体里。

1
2
3
<body>
   <form> . . . .  </form>
    <script type="text/javascript" src="<%= My.Working.Common.Util.GetSiteLocation()%>Scripts/Common.js"> </body>

现在,有关标记的冲突将得到解决。

解决方案2:

我们也可以解决上面的一个解决方案,比如用<%代替<%=替换代码块,但问题是它只给出相对路径。如果你真的想要绝对路径,它是行不通的。

解决方案1适用于我。接下来是你的选择。


另一种方法是使用另一个.aspx页面作为要链接到的页面。

母版页的标题如下:

1
2
3
4
5
<head runat="server">
   
    </asp:ContentPlaceHolder>
    <link href="CSS/AccordionStyles.aspx" rel="stylesheet" type="text/css" />
</head>

引用的.aspx表单包含您的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AccordionStyles.aspx.cs" Inherits="IntranetConnectCMS.CSS.AccordionStyles" %>
.AccordionHeader
{
    cursor: pointer;
    background-image: url(<%=ResolveUrl("~/Images/Backgrounds/AccordionPaneHeaderClosed.png") %>);
    background-repeat: no-repeat;
}

.AccordionHeaderSelected
{
    cursor: pointer;
    background-image: url(<%=ResolveUrl("~/Images/Backgrounds/AccordionPaneHeaderOpen.png") %>);
    background-repeat: no-repeat;
}
.AccordionContent
{
    background-image: url(<%=ResolveUrl("~/Images/Backgrounds/AccordionPaneContent.png") %>);
    background-repeat: no-repeat;
}

最后,您需要.aspx页面告诉浏览器您正在发送CSS内容:

1
2
3
4
protected void Page_Load(object sender, EventArgs e)
{
    Response.ContentType ="text/css";
}

我也有同样的问题,但它与JavaScript没有任何关系。考虑此代码:

1
2
3
4
5
<input id="hdnTest" type="hidden" value='<%= hdnValue %>' />
</asp:PlaceHolder>

    test content
</asp:PlaceHolder>

在这种情况下,即使占位符没有任何有害的代码块,也会得到相同的错误,这是由于非服务器控件hdnest使用代码块造成的。

只需将runat=server添加到hdnest,问题就解决了。


我的系统也有同样的问题,我从页面中删除了javascript代码,并在关闭body标记之前将其放到body中。


我通过将放在contentplaceholder内,而不是将放在所述contentplaceholder内,解决了类似的错误。


在不同的情况下,我也有同样的问题。我的身体标签里有简单的元素。解决方案是:

1
2
   ">Change me
</asp:PlaceHolder>
1
2
3
4
5
protected new void Page_Load(object sender, EventArgs e)
{    
    Page.Form.DataBind(); // I neded to Call DataBind on Form not on Header
    container.InnerHtml ="Simple text";
}

尝试将你的Java脚本代码写在头标签之外,它肯定会起作用。当我将Java脚本代码复制粘贴到页面底部时,它就解决了。在前一个中,在关闭表单标记之前,它现在放置在head标记中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
                    function validate() {
                        try {

                        var username = document.getElementById("<%=txtUserName.ClientID%>").value;
                        var password = document.getElementById("<%=txtPWD.ClientID%>").value;

                            if (username =="" && password =="")
                                alert("Enter Username and Passowrd");
                            else {
                                if (username =="")
                                    alert("Enter Username");
                                else if (password =="")
                                    alert("Enter Password");
                            }

                        }

                    catch (err) {
                    }
                }
               
</form>

如果要从代码隐藏添加动态控件,请删除具有服务器标记的部件并将其放置在其他位置

我从页面的头部删除了我的javascript,并将其添加到页面的正文中,使其正常工作。


在我的例子中,我得到了这个错误,因为我错误地将innerText设置为一个包含html的DIV。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
SuccessMessagesContainer.InnerText ="";

   
      <table>
         <tr>
            <td style="width: 80px; vertical-align: middle; height: 18px; text-align: center;">
               <img src="<%=Image_success_icn %>" style="margin: 0 auto; height: 18px;
                  width: 18px;"
/>
            </td>
            <td id="SuccessMessage" style="vertical-align: middle;" class="SuccessMessage" runat="server">
            </td>
         </tr>
      </table>

在某些情况下,resolveurl和resolveclienturl可以正常工作,但并非总是如此,特别是在JS脚本文件的情况下。发生的情况是它对某些页面有效,但当您导航到其他页面时,由于该特定页面的相对路径,它可能无法工作。

最后,我的建议是对你的站点页面进行一次全面的检查,看看你所有的javascript引用是否都是好的。在Google Chrome中打开你的站点->右键单击页面->单击查看源页面->HTML显示->现在单击你的JS超链接;如果工作正常,它应该在另一个浏览器窗口中打开JS文件,否则它不会打开。


标签<%= %>不适用于带有runat="server"的标签。将带有<%= %>的代码移到runat="server"的另一个标记(bodyhead…)中,或者从容器中删除runat="server"