我在ASP.net中創建了一個自定義控件,其中包含一個AutoDownBack設置爲true的DropDownList。我還爲控件附加了一個事件SelectedIndexChanged,但我發現該事件未被觸發。正常的asp.net DropDownList與AutoPostBack設置爲true將觸發事件。我在VS2010中使用.net 3.5版本。AutoPostBack不能用於自定義控件
這裏是我的代碼: ServerControl1.cs
[ToolboxData("<{0}:ServerControl2 runat=server></{0}:ServerControl2>")]
public class ServerControl1 : WebControl
{
protected DropDownList drplist;
protected TextBox tb;
protected override void RenderContents(HtmlTextWriter output)
{
drplist.RenderControl(output);
tb.RenderControl(output);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
drplist = new DropDownList() { AutoPostBack = true };
drplist.Items.Add("a");
drplist.Items.Add("b");
drplist.Items.Add("c");
drplist.Items.Add("d");
drplist.SelectedIndexChanged += new EventHandler(drplist_SelectedIndexChanged);
tb = new TextBox();
}
void drplist_SelectedIndexChanged(object sender, EventArgs e)
{
tb.Text = "even fired";
}
}
ServerControl1Test:
protected void Page_Load(object sender, EventArgs e)
{
ServerControl1 ServerControl1 = new ServerControl1();
this.Controls.Add(ServerControl1);
}
誰能告訴我我在這裏失蹤?