private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Write(GetControlsAttribute());
} public ControlCollection GetControls(){
ControlCollection cc = this.CreateControlCollection();
for (int i = 0; i < Page.Controls.Count;i++){
if (Page.Controls[i].ID != null){
cc.Add(Page.Controls[i]);
}
foreach(Control control in Page.Controls[i].Controls){
if (control.ID != null){
cc.Add(control);
}
}
}
return cc;
} public string GetControlsAttribute(){
string strControl = "";
ControlCollection cc = GetControls();
for (int i = 0; i < cc.Count;i++){
strControl += strControl + cc[i].ID.ToString() + ":" + cc[i].GetType().ToString() + ",";
}
return strControl;
}
Exception Details: System.InvalidOperationException: The list that this enumerator is bound to has been modified. An enumerator can only be used if the list doesn't change.Line 62: foreach(Control control in Page.Controls[i].Controls){
{
// 在此处放置用户代码以初始化页面
Response.Write(GetControlsAttribute());
} public ControlCollection GetControls(){
ControlCollection cc = this.CreateControlCollection();
for (int i = 0; i < Page.Controls.Count;i++){
if (Page.Controls[i].ID != null){
cc.Add(Page.Controls[i]);
}
foreach(Control control in Page.Controls[i].Controls){
if (control.ID != null){
cc.Add(control);
}
}
}
return cc;
} public string GetControlsAttribute(){
string strControl = "";
ControlCollection cc = GetControls();
for (int i = 0; i < cc.Count;i++){
strControl += strControl + cc[i].ID.ToString() + ":" + cc[i].GetType().ToString() + ",";
}
return strControl;
}
Exception Details: System.InvalidOperationException: The list that this enumerator is bound to has been modified. An enumerator can only be used if the list doesn't change.Line 62: foreach(Control control in Page.Controls[i].Controls){
int j=0;while (j < Page.Controls[i].Controls.Count)
{
Control control = Page.Controls[i].Controls[j];
if (control.ID != null) cc.Add(control);
else j++;
}
ControlCollection cc = this.CreateControlCollection();
for (int i = 0; i < Page.Controls.Count;i++){
if (Page.Controls[i].ID != null){
cc.Add(Page.Controls[i]);
int j = 0;
while(j < Page.Controls[i].Controls.Count){
Control control = Page.Controls[i].Controls[j];
if (control.ID != null) cc.Add(control);
else j++;
}
}
}
return cc;
} public string GetControlsAttribute(){
string strControl = "";
ControlCollection cc = GetControls();
for (int i = 0; i < cc.Count;i++){
strControl += strControl + cc[i].ID.ToString() + ":" + cc[i].GetType().ToString() + ",";
}
return strControl;
}这样GetControlsAttribute()方法获得的值为空,但实际上我页面上有很多控件,包括自定义控件,用户控件,web控件
{
sb.AppendFormat("ID:{0}:Type:{1} --- ", c.ID, c.GetType().Name);
foreach(Control cc in c.Controls)
GetControlsInfo(cc,sb);
}
...System.Text.StringBuilder sb = new System.Text.StringBuilder();
GetControlsInfo(Page,sb);
Response.Write(sb.ToString());