遍历Page中所有控件的一个奇怪问题
今天调试程序时发现一个很奇怪的问题,当在onload中遍历所有的控件时,会引起gridview的视图状态丢失,后来经过多次试验,发现代码:
protected List<Control> Components
{
get
{
List<Control> AList = new List<Control>();
AddControls(this,AList);
return AList;
}
}
/// <summary>
/// 增加一个控件和它的子控件到数组中
/// </summary>
/// <param name="AList"></param>
private void AddControls(Control AControl,List<Control> AList)
{
if( AControl.HasControls()) //<===这行代码不加就会丢失视图状态,太奇怪了
{
foreach(Control AChild in AControl.Controls)
{
AList.Add(AChild);
AddControls(AChild,AList);
}
}
}