我有一个 ASP DropDown,声明如下:
<asp:DropDownList ID="DropDown1"
runat="server"
OnSelectedIndexChanged="DropDown1_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
数据是这样填的:
DropDown1.DataSource = source;
DropDown1.DataTextField = "Key";
DropDown1.DataValueField = "Value";
DdlCat1.DataBind();
“来源”是一个字典:
Dictionary<int, string> source = new Dictionary<int, string>();
selectedIndexChanged 方法如下所示:
public void DropDown1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList temp = (DropDownList)sender;
Console.WriteLine(temp.SelectedValue);
}
现在假设我的下拉列表中有 item1、item2 和 item3。目前我选择了item1。现在的问题是,如果我单击 item2,将触发 SelectIndexChanged 方法,但 SelectedValue 始终停留在“item1”。那么,我做错了什么?
最佳答案
你的数据填在哪里?如果您的数据在页面加载或其他情况下被填充,那么由于页面生命周期,您每次都会覆盖您的数据源
关于c# - 触发 SelectedItem 事件时,SelectedItem 不会更改(ASP.Net DropDown),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8432685/