事件在 DropDownList ASP.NET 中不起作用
<asp:DropDownList runat="server" ID="DropDownList1" Width="100%" AutoPostBack="false" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"/>
事件:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach(ListItem item in DropDownList1.Items)
{
Console.WriteLine(item.Text + " | " + item.Value);
}
}
DropDownList 是这样填充的:
for(int i=0; i<5; i++){
ListItem item = new ListItem();
item.Text = "asdasd"+i;
item.Value = i;
DropDownList1.Items.Add(item);
}
尝试
AutoPostBack="true"问题解决了。在页面上启用 AJAX。