Search code examples
asp.net.netajaxcontroltoolkit

ASP.NET trying to compile commented lines of code. Why?


I have code like this on some aspx page in my Web Application:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"/>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
    <div>
        <asp:Label ID="lbl1" runat="server" Text="Laber1" />
        <asp:Button ID="btn1" runat="server" Text="Button1" onclick="btn1_Click" />
    </div>
    </ContentTemplate>
</asp:UpdatePanel>

<!--
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
<asp:TextBox ID="txtAjaxToolkit" runat="server" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtAjaxToolkit" /> 
-->
</asp:Content>

When I compile the project and go to this page in the browser, I get this exception:

[InvalidOperationException: На страницу можно добавить только один экземпляр ScriptManager.] 

(translate: "There only one instance of ScriptManager allowed on this page").

Why do I get this message if second script manager (ToolkitScriptManager) is located in comments?

I am using ASP.NET 4 and ASP.NET Ajax Control Toolkit.

P.S: Sorry for my english.


Solution

  • You've used HTML comments, but the markup engine doesn't care about that - it'll still try to generate the content of that HTML comment. After all, you might want something like a timestamp in a comment.

    You want a server-side comment, like this;

    <%--
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
    <asp:TextBox ID="txtAjaxToolkit" runat="server" />
    <asp:CalendarExtender ID="CalendarExtender1" runat="server" 
                          TargetControlID="txtAjaxToolkit" /> 
    --%>
    

    It's all a matter of working out what's going to figure out that something is a comment - the HTML <!-- comment --> is aimed at a browser; the `<%-- comment --%> is aimed at the ASPX processor.