发现ASP.Net的一个问题
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;
public ArrayList Array1;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack==false)
{
Array1=new ArrayList();
for (int i=0;i<10;i++)
{
Array1.Add(i.ToString());
DropDownList1.Items.Add(i.ToString());
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
int x1=DropDownList1.Items.Count;
int x2=Array1.Count;
////////////////////////////////////
///你会惊奇的发现x1!=x2
///可是Array1和DropDownList1是同时添加元素的,这是为什么呢?
///哈哈,你会发现页面元素是没有实力化 (New)过程的
}
}
为什么同时添加的页面控件元素和数组元素,结果他们在postback后结果居然不同,这可能是因为页面元素没有new过程
恩也就有个比较独特的性质。