[C#] “컨트롤에 코드 블록이 포함되어 있으므로 Controls 컬렉션을 수정할 수 없습니다”

슬라이더 인 간단한 사용자 정의 컨트롤을 만들려고합니다. AjaxToolkit SliderExtender를 사용자 정의 컨트롤에 추가하면 (* & $ # () @ # 오류가 발생합니다.

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\Release\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:293 AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) in d:\E\AjaxTk-AjaxControlToolkit\Release\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

자리 표시자를 사용자 정의 컨트롤에 넣고 텍스트 상자와 슬라이더 익스텐더를 프로그래밍 방식으로 자리 표시 자에 추가하려고 시도했지만 여전히 오류가 발생합니다.

간단한 코드는 다음과 같습니다.

<table cellpadding="0" cellspacing="0" style="width:100%">
    <tbody>
        <tr>
            <td></td>
            <td>
                <asp:Label ID="lblMaxValue" runat="server" Text="Maximum" CssClass="float_right" />
                <asp:Label ID="lblMinValue" runat="server" Text="Minimum" />
            </td>
        </tr>
        <tr>
            <td style="width:60%;">
                <asp:CheckBox ID="chkOn" runat="server" />
                <asp:Label ID="lblPrefix" runat="server" />:&nbsp;
                <asp:Label ID="lblSliderValue" runat="server" />&nbsp;
                <asp:Label ID="lblSuffix" runat="server" />
            </td>
            <td style="text-align:right;width:40%;">

                    <asp:TextBox ID="txtSlider" runat="server" Text="50" style="display:none;" />
                    <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>

무엇이 문제입니까?



답변

먼저 <% = 대신 <% #로 코드 블록을 시작하십시오.

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

코드 블록을 Response.Write 코드 블록에서 데이터 바인딩 식으로 변경합니다. 데이터 바인딩 표현식은 코드 블록이 아니기
때문에 <%# ... %>CLR은 불평하지 않습니다. 그런 다음 마스터 페이지의 코드에서 다음을 추가하십시오.

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


답변

방금이 문제에 부딪 쳤지 만 다른 해결책을 찾았습니다.

asp : PlaceHolder-tag로 코드 블록을 래핑하면 문제가 해결됩니다.

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

(내가 사용하는 CMS는 일부 코드에서 헤드 섹션에 삽입되어 메타 태그 등과 같은 다양한 정보가 포함 된 사용자 정의 제어 블록을 추가 할 수 없도록 제한했기 때문에 이것이 나를 위해 작동하는 유일한 방법입니다.)


답변

<% %>태그가 있는 자바 스크립트를 헤드에서 양식 태그로 이동하면이 오류가 해결 된다는 것을 확인할 수 있습니다

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


답변

자바 스크립트를 div 태그 아래에 놓습니다.

<div runat="server"> //div tag must have runat server
  //Your Jave script code goes here....
</div>

작동합니다 !!


답변

페이지에서 스크립트 관리자를 사용하는 경우 동일한 기능을 수행 할 수 있습니다. 다음과 같이 스크립트를 등록해야합니다.

<asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true"   EnablePageMethods="true">
<Scripts>
      <asp:ScriptReference Path="~/Styles/javascript/jquery.min.js" />
</Scripts>
</asp:ScriptManager>


답변

사용자 컨트롤에서 동일한 문제가 발생했습니다. 컨트롤을 호스팅하는 내 페이지에는 헤드 태그에 주석이 있었고 그 주석을 제거했습니다. 모든 것이 나중에 작동했습니다. 일부 게시물은 스크립트를 헤드에서 제거하여 본문에 배치 할 것을 제안합니다.


답변

나는 <%# %>성공하지 않고 사용해 보았습니다 . 그런 다음 Page.Header.DataBind();코드를 변경 하고 this.Header.DataBind();정상적으로 작동했습니다.