Getting error: "Control Collection Can Not Be Modified"
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..
Googling the quoted error returned an entire page of exact matches for that phrase, the first one summing up the problem quite nicely:
- The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)
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.
- “The Controls collection cannot be modified because the control contains code blocks”
精彩评论