想不通,请教--在OnLoad里调用Controls.Addat()影响了Dropdownlist的返回值
因为想在每页自动添加一个head控件,所以改写了page的OnLoad事件,然后发现页面上的DropDownlist的返回值出了问题。请高手指点一二。故障现象有点复杂,我试着尽量描述清楚些。
如果页面有个dropdownlist控件,里面的items是在Page_Load()里面添加的,在点击页面的按钮时,这些添加的items能继续保存,并能通过dropdownlist1.SelectedIndex访问它的当前选择项。
可是我修改了OnLoad事件,加了一句语句后,就不正常了,按钮提交后,dropdownlist1的items是空的,selectedindex=-1。如果没有controlsAddAt(1,head)这句,或者改为controlsAdd(head),dropdownlist1就表现正常
代码如下
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack){
this.DropDownList1.Items.Add("aaaa");
this.DropDownList1.Items.Add("bbbb");
}
}
override protected void OnLoad(EventArgs e)
{
base.OnLoad(e);
Web.Controls.Header head=new Web.Controls.Header();
Controls.AddAt(1,head);
}
#region Web Form Designer generated code
这中间的代码就不贴了
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
(DropDownList1.SelectedIndex.ToString());
this.Label1.Text = this.DropDownList1.SelectedIndex.ToString();
}
}