Controls collection Errorhttp://forums.asp.net/t/1039017.aspx/1?Controls+collection+ErrorSun, 30 May 2010 07:51:20 -040010390171442171http://forums.asp.net/p/1039017/1442171.aspx/1?Controls+collection+ErrorControls collection Error <p>When I add the following line of code in my .aspx file</p> <p>&nbsp;&lt;% Response.Write(Session(<span class="st">&quot;copyrightDate&quot;</span>))%&gt;</p> <p>I get the following error when I try to run it</p> <p>Is there a fix for this?</p> <p>&nbsp;</p> <pre class="prettyprint">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. &lt;% ... %&gt;). 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 = (&quot;document.getElementById('&quot; _ Line 110: &#43; (_viewState.ClientID &#43; (&quot;').value = GetViewState__AspNetTreeView('&quot; _ 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. &lt;% ... %&gt;).] System.Web.UI.ControlCollection.Add(Control child) &#43;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() &#43;67 System.Web.UI.Control.SaveViewStateRecursive() &#43;194 System.Web.UI.Control.SaveViewStateRecursive() &#43;194 System.Web.UI.Control.SaveViewStateRecursive() &#43;194 System.Web.UI.Page.SaveAllState() &#43;595 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) &#43;6647</pre>&nbsp;&nbsp; <p>&nbsp;</p> &nbsp;&nbsp; 2006-10-26T08:26:17-04:001442822http://forums.asp.net/p/1039017/1442822.aspx/1?Re+Controls+collection+ErrorRe: Controls collection Error <p>Instead of using:</p> <p>&nbsp;&lt;% Response.Write(Session(&quot;copyrightDate&quot;))%&gt;</p> <p>Try using:</p> <p>&nbsp;&lt;%# Session(&quot;copyrightDate&quot;) %&gt;</p> <p>&nbsp;</p> 2006-10-26T18:06:13-04:001443255http://forums.asp.net/p/1039017/1443255.aspx/1?Re+Controls+collection+ErrorRe: Controls collection Error <p>When I use&nbsp; &lt;%# Session(&quot;copyrightDate&quot;) %&gt; on my master page I dont get an error but it does not display anything</p> <p>When I use&nbsp; &lt;% Response.Write(Session(&quot;copyrightDate&quot;))%&gt; 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</p> <p>This is how i delcared the session in the page load event of the master page</p> <p>'''''' Current date and time.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim currDate As Date = Now<br> <br> ''''''Get only the year of the date<br> &nbsp;&nbsp;&nbsp;&nbsp; Session(&quot;copyrightDate&quot;) = DatePart(DateInterval.Year, currDate) <br> </p> 2006-10-27T00:43:33-04:001443260http://forums.asp.net/p/1039017/1443260.aspx/1?Re+Controls+collection+ErrorRe: Controls collection Error 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 &lt;% %&gt; in your aspx file<br> 2006-10-27T00:51:11-04:001443264http://forums.asp.net/p/1039017/1443264.aspx/1?Re+Controls+collection+ErrorRe: Controls collection Error <p>Actually, now that I think about it, I would recommend that you replace your &lt;% whatever %&gt; with something like this instead,</p> <p>&lt;asp:Label runat=&quot;server&quot; id=&quot;myLabel&quot; Text=&quot;&lt;%# Session(&quot;copyrightDate&quot;) %&gt;&quot; /&gt;</p> <p>And then in your page's Page_Load method you would be sure to do this:</p> <p>myLabel.DataBind()</p> <p>Does that make sense?</p> 2006-10-27T00:53:43-04:001443275http://forums.asp.net/p/1039017/1443275.aspx/1?Re+Controls+collection+ErrorRe: Controls collection Error yes that makes sense...thanx a lot<br> 2006-10-27T01:05:02-04:003882965http://forums.asp.net/p/1039017/3882965.aspx/1?Re+Controls+collection+ErrorRe: Controls collection Error <p></p> <p></p> &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Hello,&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;I had the same problem.&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;The cause of this error are code blocks with '=' signs in Markup page [&lt;%= %&gt;]. Try to find them in markup page and move them to code behind;&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;e.g.:&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Markup: &lt;a href='&lt;%= ResolveUrl(&quot;~/url.ext&quot;)%&gt;'&gt;Link Text&lt;/a&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Replace above with:&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;a id=&quot;link1&quot; runat=&quot;server&quot;&gt;Link Text&lt;/a&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Code Behind:&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;link1.ResolveUrl(&quot;~/url.ext&quot;);&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Attention: Your markup tag should contain both ID=&quot;anyID&quot; and RUNAT=&quot;SERVER&quot; to be accessible in code behind.&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;May Allah bless you all,&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 100px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;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]&lt;/div&gt; <p></p> <p>Hello,</p> <p>I had the same problem.</p> <p><br> </p> <p>The cause of this error are code blocks with '=' signs in Markup page [&lt;%= %&gt;]. Try to find them in markup page and move them to code behind;&nbsp;</p> <p>e.g.:</p> <p>Markup: &lt;a href='&lt;%= ResolveUrl(&quot;~/url.ext&quot;)%&gt;'&gt;Link Text&lt;/a&gt;</p> <p><br> </p> <p>Replace above with:</p> <p>&lt;a id=&quot;link1&quot; runat=&quot;server&quot;&gt;Link Text&lt;/a&gt;</p> <p>Code Behind:</p> <p>link1.ResolveUrl(&quot;~/url.ext&quot;);</p> <p><br> </p> <p>Attention: Your markup tag should contain both ID=&quot;anyID&quot; and RUNAT=&quot;SERVER&quot; to be accessible in code behind.</p> <p><br> </p> <p>May Allah bless you all,</p> <p><br> </p> <p>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]</p> <p></p> <p></p> <p></p> 2010-05-30T07:51:20-04:00