【发布时间】:2011-04-04 00:54:57
【问题描述】:
我的代码中有以下 DropDownList 以错误的顺序触发:
public class MyWebpart : WebPart
{
private DropDownList dropDown = new DropDownList();
private string selectedValue;
public Webpart()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dropDown.AutoPostBack = true;
dropDown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnsureChildControls();
}
protected void DropDown_SelectedIndexChanged(Object sender, EventArgs e)
{
selectedValue - dropDown.SelectedValue;
}
protected void override void CreateChildControls()
{
base.CreateChildControls();
// create some stuff here
}
我期待当下拉选择更改时,DropDown_SelectedIndexChanged 将首先被调用,但它经历了从 OnInit、OnLoad、CreateChildControls 到 DropDown_SelectedIndexChanged 的整个生命周期。
我错过了什么吗?如何先获取 DropDown_SelectedIndexChanged 调用?
【问题讨论】:
-
你的问题和C#有什么关系?
-
正如 Tahbaza 所说,生命周期行为不能轻易改变。也许您可以详细说明为什么这个生命周期是一个问题?