-2

Hi i am initially using this code but now when i am debuging this code i am getting error ..

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

my code is:

for (int m = 0; m < dtGroupedByDate.Rows.Count; m++)
{
    Label Date = new Label();
    Date.Text = dtGrpBySmDate.Rows[m][0].ToString();
    Date.Style["margin-left"] = (m > 0) ? "20px" : "0px";
    this.Controls.Add(Date);
    Label PowerSum = new Label();
    PowerSum.Text = dtGroupedByDate.Rows[m][1].ToString();
    PowerSum.Style["margin-left"] = "20px";
    this.Controls.Add(PowerSum);
}

Please help me why i am getting this error..

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Forums Forall
  • 37
  • 1
  • 10
  • 2
    I suspect this is not the source of your error, can you post the markup ? – V4Vendetta Jul 18 '11 at 11:49
  • *Please* try and do some more research before posting questions, for example if you have an error then try and Google the error text - I know that you haven't already tried this because if you had, you would have known to post some markup too. – Justin Jul 18 '11 at 11:56
  • @Kragen you are right Thanks man and @V4Vendetta if you dont know any thing then please dont reply dont let the question to negative side! – Forums Forall Jul 18 '11 at 12:03
  • @Forums You have taken it in the wrong spirit, i precisely asked you to post the markup so that its evident at any point of time as what was causing the issue. – V4Vendetta Jul 18 '11 at 12:07
  • @V4Vendetta first thing if i know the issue would i put my question there and the second thing is that my this code is running fine on one page but it gives me error on this code for some other page thats why i was amazed but Kragen was right it helps me out – Forums Forall Jul 19 '11 at 05:36

1 Answers1

2

Googling the quoted error returned an entire page of exact matches for that phrase, the first one summing up the problem quite nicely:

Basically it looks like if you use the <% ... %> syntax anywhere in your .aspx page, modification of the Controls collection in this way will fail.

The page then explains that you can work around this by using the data binding syntax <%# ... %> instead, but this might not always be possible depending on the situation.

The second link is a fairly comprehensive StackOverflow question on the subject that may also be helpful.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367