我有一个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();
“source”是一本字典:
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”。那么,我做错了什么?
答案 0 :(得分:5)
您的数据填写在哪里?如果您的数据是在页面加载时填充的,那么由于页面生命周期,您每次都会覆盖数据源