简体   繁体   中英

SelectedIndexChanged doesn't work!

My code:

*.aspx:

<asp:DropDownList ID="CountryList" CssClass="CountryList" runat="server" 
           OnSelectedIndexChanged="CountryList_SelectedIndexChanged" />

*.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
   CountryList.SelectedIndexChanged += 
                          new EventHandler(CountryList_SelectedIndexChanged);
   ...  
}

protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
   LoadCityList(CountryList, CityList);
}

But this doesn't work.

Try setting AutoPostBack="true" on this dropdown list:

<asp:DropDownList 
    ID="CountryList" 
    CssClass="CountryList" 
    runat="server" 
    OnSelectedIndexChanged="CountryList_SelectedIndexChanged"
    AutoPostBack="true"  
/>

Also you don't need to manually wire-up the event handler in the Page_Load method. It will be automatically done by ASP.NET when it compiles the webform:

protected void Page_Load(object sender, EventArgs e)
{
    ... 
}

protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
    LoadCityList(CountryList, CityList);
}

我想你错过了aspx文件中的AutoPostBack =“true”属性

在你的aspx代码中添加AutoPostBack =“true”,一切都会像你想象的那样工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM