3

我的代码:

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

但这不起作用。

4

3 回答 3

13

尝试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);
}
于 2011-04-26T08:42:35.433 回答
1

我认为您错过了 aspx 文件中的 AutoPostBack="true" 属性

于 2011-04-26T08:44:21.880 回答
1

在您的 aspx 代码中添加 AutoPostBack="true",一切都会如您所想。

于 2011-04-26T08:46:14.480 回答