locked
Controls collection Error RRS feed

  • Question

  • User1687789121 posted

    When I add the following line of code in my .aspx file

     <% Response.Write(Session("copyrightDate"))%>

    I get the following error when I try to run it

    Is there a fix for this?

     

    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:
    
    Line 106:                    Dim panel As Panel = New Panel()
    Line 107:                    panel.Controls.Add(_viewState)
    Line 108:                    Page.Form.Controls.Add(panel)
    Line 109:                    Dim script As String = ("document.getElementById('" _
    Line 110:                                + (_viewState.ClientID + ("').value = GetViewState__AspNetTreeView('" _
    
    
    Source File: C:\Documents and Settings\Brian Jones\My Documents\Visual Studio 2005\cis281\App_Code\Adapters\TreeViewAdapter.vb    Line: 108
    
    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) +233
       CSSFriendly.TreeViewAdapter.SaveAdapterViewState() in C:\Documents and Settings\Brian Jones\My Documents\Visual Studio 2005\cis281\IdeaSystems\App_Code\Adapters\TreeViewAdapter.vb:108
       System.Web.UI.Control.SaveViewStateRecursive() +67
       System.Web.UI.Control.SaveViewStateRecursive() +194
       System.Web.UI.Control.SaveViewStateRecursive() +194
       System.Web.UI.Control.SaveViewStateRecursive() +194
       System.Web.UI.Page.SaveAllState() +595
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6647
    
      

     

      
    Thursday, October 26, 2006 4:26 AM

All replies

  • User-534056067 posted

    Instead of using:

     <% Response.Write(Session("copyrightDate"))%>

    Try using:

     <%# Session("copyrightDate") %>

     

    Thursday, October 26, 2006 2:06 PM
  • User1687789121 posted

    When I use  <%# Session("copyrightDate") %> on my master page I dont get an error but it does not display anything

    When I use  <% Response.Write(Session("copyrightDate"))%> on a .aspx page I dont get an error but it does display the date, but I dont want it on a .aspx page that uses the master...I want it to go in the footer of the master page

    This is how i delcared the session in the page load event of the master page

    '''''' Current date and time.
            Dim currDate As Date = Now

    ''''''Get only the year of the date
         Session("copyrightDate") = DatePart(DateInterval.Year, currDate)

    Thursday, October 26, 2006 8:43 PM
  • User1687789121 posted
    Don't worry about my problem...I'm just gonna create a label and set the text equal to currDate...but I would like to why can't you use <% %> in your aspx file
    Thursday, October 26, 2006 8:51 PM
  • User-534056067 posted

    Actually, now that I think about it, I would recommend that you replace your <% whatever %> with something like this instead,

    <asp:Label runat="server" id="myLabel" Text="<%# Session("copyrightDate") %>" />

    And then in your page's Page_Load method you would be sure to do this:

    myLabel.DataBind()

    Does that make sense?

    Thursday, October 26, 2006 8:53 PM
  • User1687789121 posted
    yes that makes sense...thanx a lot
    Thursday, October 26, 2006 9:05 PM
  • User430339774 posted

    Hello,
    I had the same problem.
    The cause of this error are code blocks with '=' signs in Markup page [<%= %>]. Try to find them in markup page and move them to code behind; 
    e.g.:
    Markup: <a href='<%= ResolveUrl("~/url.ext")%>'>Link Text</a>
    Replace above with:
    <a id="link1" runat="server">Link Text</a>
    Code Behind:
    link1.ResolveUrl("~/url.ext");
    Attention: Your markup tag should contain both ID="anyID" and RUNAT="SERVER" to be accessible in code behind.
    May Allah bless you all,
    As for those who believe and do good works, He will pay them their wages in full. Allah does not love the harm doers. [Koran 3.57]

    Hello,

    I had the same problem.


    The cause of this error are code blocks with '=' signs in Markup page [<%= %>]. Try to find them in markup page and move them to code behind; 

    e.g.:

    Markup: <a href='<%= ResolveUrl("~/url.ext")%>'>Link Text</a>


    Replace above with:

    <a id="link1" runat="server">Link Text</a>

    Code Behind:

    link1.ResolveUrl("~/url.ext");


    Attention: Your markup tag should contain both ID="anyID" and RUNAT="SERVER" to be accessible in code behind.


    May Allah bless you all,


    As for those who believe and do good works, He will pay them their wages in full. Allah does not love the harm doers. [Koran 3.57]

    Sunday, May 30, 2010 3:51 AM