3

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.

Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
roman
  • 199
  • 2
  • 2
  • 9

3 Answers3

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);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @roman, of course, that's the purpose of AutoPostBack. What is it that you are trying to achieve and what's the problem with the page reloading? If you want to use AJAX you could put this dropdown in an UpdatePanel. – Darin Dimitrov Apr 26 '11 at 08:52
1

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

Access Denied
  • 886
  • 1
  • 13
  • 24
  • 1
    why is this necessary? The method exists to do things on changing the selected value. Either this property should be added by default, or shouldn't be required... – muttley91 Jul 12 '13 at 18:18
1

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

AbhiRoczz...
  • 254
  • 2
  • 13