FieryA
FieryA

Reputation: 297

“The Controls collection cannot be modified because the control contains code blocks”

While trying to insert controls in my page with the following block of code, i got this exception

The Controls collection cannot be modified because the control contains code blocks

Literal lite=new Literal();
lite.Text = @"<div class='cookieLaw_slidesharey'><a id='cookieLaw_slideshare' href='#'><img src='/img/content/cookielaw_slideshare.jpg'/></a></div>";
Control control = this.FindControl("PlaceHolder");
control.Controls.AddAt(0,lite);

I tried to follow the advices written in this thread:

"The Controls collection cannot be modified because the control contains code blocks"

Which are basically:

replacing <%= %> with <%# %>

And adding

Page.Header.Databind() in the page load

But i get the following exception:

Object reference not set to an instance of an object.

Thank you for your help.

PS: i'm using .NET 3.5

Upvotes: 0

Views: 1898

Answers (1)

Gatogordo
Gatogordo

Reputation: 2635

Check if your "PlaceHolder" has a runat=server attribute. That should already help. You can address that control by it's id then instead of using FindControl.

The reason why your Page.Header.Databind() is not working is probably because you don't have a head section with the runat=server like in the example you copied from. They were adding controls in the header there which can be slightly different.

Upvotes: 1

Related Questions