我有一个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”。那么,我在做什么错呢?

最佳答案

您的数据在哪里填充?如果您的数据是在页面加载时填充的,则由于页面生命周期的原因,您每次都会覆盖数据源

07-28 07:32