c# - The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>) -


i stuck error , found work around , solutions works me, i know best way fix issue , want make sure wont affect other pages badly. hope experts help. if best solution many of can save heads.

this error occurs when code block placed in masterpage. place code block in placeholder resolve issue.when adding ajax extenders web page, attempt register scripts in head. if code blocks present in masterpage, error might occur.

to resolve issue, move code block placeholder in head of masterpage, so:

<head id="head1" runat="server">     <title>untitled page</title>     <link href="stylesheet.css" rel="stylesheet" type="text/css" />     <asp:contentplaceholder id="myplaceholder" runat="server">     <script language="javascript" type="text/javascript" src="<%= page.resolveclienturl("~/javascript/global.js")%>"></script>     </asp:contentplaceholder>     <asp:contentplaceholder id="head" runat="server"></asp:contentplaceholder> </head> 

the error logical can not confuse rendered controls after rendered using <%= %>

one way solve issue use literal control, , render script line on code behind.

<asp:contentplaceholder id="myplaceholder" runat="server">     <asp:literal runat="server" id="txtincludescript" enableviewstate="false"></asp:literal> </asp:contentplaceholder> 

and on code behind. check null because if change placeholder literal null. set enableviewstate=false because set on every page_load , not wish save viewstate.

  if(txtincludescript != null)   {     txtincludescript.text =  string.format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>",    page.resolveclienturl("~/javascript/global.js"));   } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -