SelectedIndexChanged不起作用

本文关键字:不起作用 SelectedIndexChanged | 更新日期: 2023-09-27 17:52:52

我的代码:

* . 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);
}

但这不起作用。

SelectedIndexChanged不起作用

尝试在下拉列表中设置AutoPostBack="true":

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

您也不需要在Page_Load方法中手动连接事件处理程序。它将由ASP自动完成。. NET编译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",一切都会如你所想。