SelectedIndexChanged doesn't work!

2019-02-25 04:41发布

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.

3条回答
你好瞎i
2楼-- · 2019-02-25 05:13

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);
}
查看更多
祖国的老花朵
3楼-- · 2019-02-25 05:34

Add AutoPostBack="true" in ur aspx code and everything will work as you thought.

查看更多
家丑人穷心不美
4楼-- · 2019-02-25 05:35

I think you missed AutoPostBack="true" Property in aspx file

查看更多
登录 后发表回答