Last post Apr 06, 2018 10:35 AM by knowledgist
Apr 06, 2018 07:13 AM|knowledgist|LINK
I have three pages:
I added MyUserControl.ascx web user control in Default.aspx programmatically on the page load event of default.aspx
Following is my code:
' find the place holder
Dim mainContent As ContentPlaceHolder = CType(Master.FindControl("PlaceHolder1"), ContentPlaceHolder)
' load the control
Dim MyControl As Control = CType(LoadControl("~/UserControls/MyUserControl.ascx"), Control)
' add the control
Whenever I run my page, it returns me the following error:
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. <% ... %>).
Line 23: ' add the control
Line 24: mainContent.Controls.Add(MyControl)
Line 26: End If
Don't understand where I am doing mistake.
Apr 06, 2018 08:34 AM|KathyW|LINK
The error says: " The
Controls collection cannot be modified because the control contains code blocks "
Does it? Google that error and you'll find thread such as this one:
https://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl which gives several work-arounds, and, part way through, an explanation for why.
Apr 06, 2018 10:35 AM|knowledgist|LINK
Thanks for reply,
I have gone through with the shared URL and other also, even found a similar example and I did the exactly same what they did in their examples but no success.
Even I added a blank user control with no markup or code behind but the error is same.