Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
ASP.NET
<asp:DropDownList ID="countrylist" runat="server" 
                    onselectedindexchanged="countrylist_SelectedIndexChanged" AutoPostBack="True">
               </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style5">
                    state</td>
                <td class="style6">
                            
                    <asp:DropDownList ID="statelist" runat="server"  
                        onselectedindexchanged="statelist_SelectedIndexChanged" AutoPostBack="True">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    City</td>
                <td>
                            
                    <asp:DropDownList ID="city" runat="server" 
                        onselectedindexchanged="city_SelectedIndexChanged" AutoPostBack="True">
                    </asp:DropDownList>




C#
namespace tables
{
    public partial class CompanyRegi : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               
                Contrydropdown();
 
                
            }
        }
 

        protected void Contrydropdown()
        {
            //conenction path for database
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Country", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
 

 
            countrylist.DataSource = ds;
            countrylist.DataTextField = "CountryName";
            countrylist.DataValueField = "CountryID";
            countrylist.DataBind();
            countrylist.Items.Insert(0, new ListItem("--Select--", "0"));
            statelist.Items.Insert(0, new ListItem("--Select--", "0"));
            city.Items.Insert(0, new ListItem("--Select--", "0"));
            con.Close();
 

        }
        /// Bind State Dropdown Based on CountryID

        protected void countrylist_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CountryID = Convert.ToInt32(countrylist.SelectedValue);
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryID, con);
 
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
 

 
            statelist.DataSource = ds;
            statelist.DataTextField = "StateName";
            statelist.DataValueField = "StateID";
            statelist.DataBind();
            statelist.Items.Insert(0, new ListItem("--Select--", "0"));
            con.Close();
 
            if (statelist.SelectedValue == "0")
            {
                city.Items.Clear();
                city.Items.Insert(0, new ListItem("--Select--", "0"));
            }
 
        }
 
        protected void statelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            int StateID = Convert.ToInt32(statelist.SelectedValue);
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from City where StateID=" + StateID, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            city.DataSource = ds;
            city.DataTextField = "CityName";
            city.DataValueField = "CityID";
            city.DataBind();
            city.Items.Insert(0, new ListItem("--Select--", "0"));
        }
 
        protected void city_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
Posted
Updated 5-Mar-14 23:55pm
v3
Comments
Ramug10 6-Mar-14 4:07am    
are you getting any error?
jayraj86 6-Mar-14 5:52am    
No, I'm not getting any errors. the thing is when I select Country from countrylist dropdown the page doesn't refresh which should as it's auto post back. States are not coming in statelist dropdown.
Ramug10 6-Mar-14 5:56am    
do you have ajax update panel control in your designer code??
jayraj86 6-Mar-14 6:02am    
No.
V5709 6-Mar-14 7:45am    
Try to find exact point where functionality is going wrong. Use break point at 'countrylist_SelectedIndexChanged' & check is the selected country id is getting properly or not.

1 solution

Here's the update panel alternative examplified

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SampleCascade.aspx.cs" Inherits="Lunch.SampleCascade" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="AjaxScriptManager" EnablePartialRendering="True"></asp:ScriptManager>
    <div>
        <asp:UpdatePanel runat="server" ID="UpdPnl" UpdateMode="Always">
            <ContentTemplate>
                <div>
                    <label>Country</label>
                    <asp:DropDownList runat="server" ID="CountryDropDown" AutoPostBack="True" OnSelectedIndexChanged="CountryDropDownSelectedIndexChanged">
                    <asp:ListItem Value="dk" Selected="True">Denmark</asp:ListItem>
                    <asp:ListItem Value="ca">Canada</asp:ListItem>
                </asp:DropDownList>
                </div>
                <div>
                    <label>City</label>
                    <asp:DropDownList runat="server" ID="CityDropDown" AutoPostBack="True" OnSelectedIndexChanged="CityDropDownSelectedIndexChanged">
                    </asp:DropDownList>
                </div>
                <asp:Label runat="server" ID="Feedback"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>

    </div>
    </form>
</body>
</html>


and with code behind

C#
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Lunch
{
    public partial class SampleCascade : System.Web.UI.Page
    {
        private Dictionary<string, HashSet<string>> _cities = new Dictionary<string, HashSet<string>>();

        protected void Page_Init(object sender, EventArgs e)
        {
            _cities.Add("dk", new HashSet<string>(new []{ "Copenhagen", "Aalborg", "Odense" }));
            _cities.Add("ca", new HashSet<string>(new [] { "Ottawa", "Toronto", "Charlottetown" }));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                LoadCities(CountryDropDown.SelectedValue);
        }

        protected void CountryDropDownSelectedIndexChanged(object sender, EventArgs e)
        {
            var ctry = (DropDownList) sender;
            LoadCities(ctry.Items[ctry.SelectedIndex].Value);
            Feedback.Text = "Loaded cities for " + ctry.Items[ctry.SelectedIndex].Text;
        }

        protected void CityDropDownSelectedIndexChanged(object sender, EventArgs e)
        {
            var city = (DropDownList) sender;
            ListItem selected = city.Items[city.SelectedIndex];
            if (selected.Value == "0") return;
            Feedback.Text = "City selected: " + selected.Text;
        }

        private void LoadCities(string country)
        {
            CityDropDown.Items.Clear();
            foreach (var itm in _cities[country])
            {
                CityDropDown.Items.Add(new ListItem(itm, itm.ToLower()));
            }
            CityDropDown.Items.Insert(0, new ListItem("Select", "0"));
        }


    }
}
 
Share this answer
 

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