【问题标题】:The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>) error无法修改 Controls 集合,因为该控件包含代码块(即 <% ... %>)错误
【发布时间】:2013-06-18 21:29:41
【问题描述】:

代码应该可以工作,但它给了我这个错误..反汇编说这个“AjaxControlToolkit.ExtenderControlBase.OnLoad(System.EventArgs)”

 var timeoutID;

    function delayedAlert() {
        document.getElementById('<%= Label3.ClientID %>').style.display = 'inherit';
            timeoutID = window.setTimeout(labelhide, 3000);
    }

    function labelhide() {
        document.getElementById('<%= Label3.ClientID %>').style.display = 'none';
    }

文本框

 <asp:Button ID="Button1" runat="server"  Onclick = "Button1_Click" 
            OnClientClick = "javascript:delayedAlert(); return SubmitForm();" 
            Text="Submit" Width="98px"/>

标签

<asp:Label ID="Label3" runat="server" Text="Entry Successful!" Visible="False" ForeColor="Lime"  ></asp:Label>

【问题讨论】:

  • UpdatePanel 里面有这个吗?
  • @Aristos 我不这么认为..
  • @user2484066 请使用“相信”这个词来表示您无法确定并且您接受与否的想法。这里我们有“我知道那在 UpdatePanel 里面,或者我知道那不是,或者我知道那在标题里面而不是在 UpdatePanel 里面”
  • @user2484066 好的,要解决这个问题,使用文字控件并在那里呈现 id 或完整的 javascript,(并删除 )。标头不喜欢它们。

标签: c# asp.net user-controls


【解决方案1】:

如果暂时忘记导致错误的原因,结果是页面抱怨无法渲染它,因为找到了&lt;%= %&gt;,它是直接将字符串渲染到页面。

一种解决方案是使用Literal 控件并在我们喜欢的代码后面呈现。

例如:

function delayedAlert() {
    document.getElementById('<asp:Literal runat="server" id="txtTheId" EnableViewState="false" />').style.display = 'inherit';
        timeoutID = window.setTimeout(labelhide, 3000);
}

以及背后的代码

txtTheId.Text = Label3.ClientID.ToString();

当然这只是绕过错误的想法,你可以在后面的代码上渲染完整的javascript,你注册脚本或者你可以把id渲染为变量。

例如:

<asp:Literal runat="server" id="txtTheId" EnableViewState="false" />    
var timeoutID;

function delayedAlert() {
    document.getElementById(Label3ID).style.display = 'inherit';
        timeoutID = window.setTimeout(labelhide, 3000);
}

function labelhide() {
    document.getElementById(Label3ID).style.display = 'none';
}

你背后的代码有

txtTheId.Text = "var Label3ID='" + Label3.ClientID + "';";

【讨论】:

  • 你能解释一下背后的代码是什么吗?代码后面的代码应该放在哪里?
  • @user2484066 你把它放在PageLoad
猜你喜欢
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 2013-08-08
  • 2011-06-27
相关资源
最近更新 更多