I have used a simple cascading(country,state,city) DropDown in my aspx page below:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="cbCountry" runat="server" AutoPostBack="True"
onselectedindexchanged="cbCountry_SelectedIndexChanged" Width="300px">
</asp:DropDownList>
<asp:DropDownList ID="cbState" runat="server" AutoPostBack="True"
onselectedindexchanged="cbState_SelectedIndexChanged" Width="300px">
</asp:DropDownList>
<asp:DropDownList ID="cbCity" runat="server" AutoPostBack="True" Width="300px">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
Below is the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataTable country = new DataTable();
using (SqlConnection con = new SqlConnection(...)
{
string cmd = "SELECT countryID, country FROM [mast_country]";
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
da.Fill(country);
cbCountry.DataSource = country;
cbCountry.DataTextField = "country";
cbCountry.DataValueField = "countryID";
cbCountry.DataBind();
}
cbCountry.Items.Insert(0, new ListItem("Select Country", "0"));
}
}
protected void cbCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int countryid = Convert.ToInt32(cbCountry.SelectedValue);
DataTable state = new DataTable();
using (SqlConnection con2 = new SqlConnection(...)
{
string cmd = "SELECT stateID, state FROM [mast_state] WHERE countryID = " + countryid;
SqlDataAdapter da = new SqlDataAdapter(cmd, con2);
da.Fill(state);
cbState.DataSource = state;
cbState.DataTextField = "state";
cbState.DataValueField = "stateID";
cbState.DataBind();
}
cbState.Items.Insert(0, new ListItem("Select State", "0"));
}
protected void cbState_SelectedIndexChanged(object sender, EventArgs e)
{
int stateid = Convert.ToInt32(cbState.SelectedValue);
DataTable city = new DataTable();
using (SqlConnection con2 = new SqlConnection(...)
{
string cmd = "SELECT cityID, city FROM [mast_city] WHERE stateID = " + stateid;
SqlDataAdapter da = new SqlDataAdapter(cmd , con2);
da.Fill(city);
cbCity.DataSource = city;
cbCity.DataTextField = "city";
cbCity.DataValueField = "cityID";
cbCity.DataBind();
}
cbCity.Items.Insert(0, new ListItem("Select City", "0"));
}
Till here everything is working fine. But I have added a gridview which contains USERS' Records and has a corresponding 'edit' button which onClick, fills the above form (it has other fields such as name, email, mobile as well) from the values stored in database table.
Everything fills just perfect but when it comes to these dropdowns, Country gets the proper value but the State and City fields shows nothing.
Please suggest apt solution(s).
Please note I have done this cascading with AJAXExtender.
And all three
DropDowns' autopostback = true;
Thanks and Regards!