我在代碼隱藏中生成下拉列表,無法讓selectedindexchanged事件自動觸發。當直接放入ASPX頁面時它工作正常,但我需要它在代碼隱藏中。ASP.NET下拉列表Codebehind vs ASPX頁面
這不起作用:
var deptList = new DropDownList
{
ID = "deptList",
DataSource = departments,
DataTextField = "deptname",
DataValueField = "deptid",
AutoPostBack = true,
EnableViewState = true
};
deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);
deptList.DataSource = departments;
deptList.DataTextField = "deptname";
deptList.DataValueField = "deptid";
if (!IsPostBack)
deptList.DataBind();
deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));
writer.Write("Select a department: ");
deptList.RenderControl(writer);
但這個工程:
<asp:DropDownList ID="deptList" AutoPostBack="true" runat="server" OnSelectedIndexChanged="deptList_SelectedIndexChanged"></asp:DropDownList>
因此,因爲我將它呈現給HTMLTexWriter,我會將它呈現給Init還是Load? – Alex 2010-02-17 16:52:44
我把整個代碼塊放到page_init中,並且什麼也沒有發生 – Alex 2010-02-17 17:39:27
比較Firebug或IE開發控制檯中工作實現(標記)和非工作(代碼)之間的標記。有些不同。 – 2010-02-17 20:34:58