tektipster79
Programmer
I've been having a lot of trouble trying to implement cascading dropdowns (select a country, and then a state) with regular asp.net dropdowns. I've posted on a few other message boards and no one seems to know the answer.
When the country dropdown selection changes, I send the selected value to a table adapter which returns the list of states. The state dropdown is not updating though. The right states are returned, I can see that when debugging, but the state dropdown never gets updated. Here's my code:
Any ideas?
When the country dropdown selection changes, I send the selected value to a table adapter which returns the list of states. The state dropdown is not updating though. The right states are returned, I can see that when debugging, but the state dropdown never gets updated. Here's my code:
Code:
<asp:UpdatePanel ID="upCountry" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<tr>
<td class="style1">
<asp:Label ID="lblCountry" runat="server"
AssociatedControlID="ddlCountry">Country:</asp:Label></td>
<td class="style2">
<asp:DropDownList ID="ddlCountry" runat="server"
AutoPostBack="true" Width="250px"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
</asp:DropDownList></td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="lblState" runat="server"
AssociatedControlID="ddlState">State/province:</asp:Label></td>
<td class="style2">
<asp:DropDownList ID="ddlState" Width="250px"
runat="server" AutoPostBack="true"></asp:DropDownList></td>
</tr>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Code:
if (!IsPostBack)
{
PopulateDropDowns();
}
else
{
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
if (ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID.ToString() == "ddlCountry")
{
PopulateDropDowns();
UpdatePanel upCountry = FindControl(CreateUserWizard1, "upCountry") as UpdatePanel;
upCountry.Update();
}
}
}
protected void PopulateDropDowns()
{
ddlCountry.Items.Insert(0, "");
foreach (DataRow dr in countries)
{
ddlCountry.Items.Add(new ListItem((string)dr["COCD_Name"],
dr["COCD_Country_CD"].ToString()));
}
ddlCountry.SelectedValue = "USA";
//omitted some adapter code here
if (filteredStates.Rows.Count == 0)
{
ddlState.Enabled = false;
}
else
{
ddlState.Enabled = true;
ddlState.Items.Clear();
ddlState.Items.Insert(0, "");
foreach (DataRow dr in filteredStates)
{
ddlState.Items.Add(new ListItem((string)dr["STCD_Descr"],
dr["STCD_State_CD"].ToString()));
}
}
}
Any ideas?