Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
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:
C#
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
HTML
DropDowns' autopostback = true;


Thanks and Regards!
Posted
Updated 10-Oct-13 1:29am
v2
Comments
thatraja 10-Oct-13 5:09am    
Include ASPX content in your question which is easy to find solution.
Rahul Vohra 10-Oct-13 7:02am    
sure
Rahul Vohra 11-Oct-13 16:43pm    
Very nice, if you don't have answer to some question, just DOWNVOTE it! Good Lord! Help Me!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900