Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Sir/Madam,
I want to select country, based on the country the corresponding states wants to retrieve... But i have got everything whatever stored in the database table...

This is my design page
ASP.NET
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
   

    <div>
        
        
            <asp:UpdatePanel ID="CountryPanel" runat="server">
                <contenttemplate>
                    <tr>
                        <td>
                            <asp:Label ID="lbl_Country" runat="server" Text="Country" Width="150px">
                        </td>
                        <td>
                            <asp:DropDownList ID="DDL_Country" runat="server" Width="150px" 
                                AutoPostBack="true" AppendDataBoundItems="true" 
                                onselectedindexchanged="DDL_Country_SelectedIndexChanged">
                            
                        </td>
                    </tr>
                </contenttemplate>
                <triggers>
                    <asp:AsyncPostBackTrigger ControlID="DDL_Country" />
                </triggers>
            
            <br />

        <asp:UpdatePanel ID="StatePanel" runat="server">
        <contenttemplate>
        <tr>
            
        <td>
        <asp:Label ID="lbl_State" runat="server" Text="State" Width="150px">
        </td>
            <asp:DropDownList ID="DDL_State" runat="server" Width="150px" 
              AutoPostBack="true" AppendDataBoundItems="true" onselectedindexchanged="DDL_State_SelectedIndexChanged">
            
        <td>
        </td>
        </tr>
        </contenttemplate>
        <triggers>
        <asp:AsyncPostBackTrigger ControlID="DDL_State" />

        </triggers>
        
        <br />
        
    </div>
    </form>
</body>


This is my code behind file
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Country();
    }
}

public void Country()
{
    con.Open();
    cmd = new SqlCommand("select CountryId,Country from Country", con);
    dr = cmd.ExecuteReader();
    DDL_Country.DataSource = dr;
    DDL_Country.Items.Clear();
    DDL_Country.Items.Add("-Select Country-");
    DDL_Country.DataTextField = "Country";
    DDL_Country.DataValueField = "CountryId";
    DDL_Country.DataBind();
    con.Close();

}

public void State()
{
    con.Open();
    cmd = new SqlCommand("select state_ID,State_Name from stat", con);
    dr = cmd.ExecuteReader();
    DDL_State.DataSource = dr;
    DDL_State.Items.Clear();
    DDL_State.Items.Add("-Select State-");
    DDL_State.DataTextField = "State_Name";
    DDL_State.DataValueField = "state_ID";
    DDL_State.DataBind();
    con.Close();

}

protected void DDL_Country_SelectedIndexChanged(object sender, EventArgs e)
{
    State();

}

Please help me...


Consider i have countries like India , Pakistan, Srilanka...
If i select India means India's states like Tamilnadu, Karnataka only want to retrieve from the database. But in my case all the states (India,Pakistan,Srilanka) retrieving from the db...

Thanks in Advance...
Posted
Updated 25-May-14 21:52pm
v2

Hello ,
select the state country wise.Instead of that you have fetched all state from your database . so, modify the query and give a Where condition in your query.

for further details on cascading drop down
Refer: Cascading Dropdown in Ajax

thanks
 
Share this answer
 
v2
public void State()
{
con.Open();
cmd = new SqlCommand("select state_ID,State_Name from stat where CountryId='CountryId'", con);
dr = cmd.ExecuteReader();
DDL_State.DataSource = dr;
DDL_State.Items.Clear();
DDL_State.Items.Add("-Select State-");
DDL_State.DataTextField = "State_Name";
DDL_State.DataValueField = "state_ID";
DDL_State.DataBind();
con.Close();

}
 
Share this answer
 
ASP.NET
<body>
    <form id="form1" runat="server">
    <asp:scriptmanager id="ScriptManager1" runat="server" xmlns:asp="#unknown">
    </asp:scriptmanager>
    <div>
        <asp:updatepanel id="CountryPanel" runat="server" xmlns:asp="#unknown">
            <contenttemplate>
                <tr>
                    <td>
                        <asp:label id="lbl_Country" runat="server" text="Country" width="150px"></asp:label>
                    </td>
                    <td>
                        <asp:dropdownlist id="DDL_Country" runat="server" width="150px" autopostback="true">
                            AppendDataBoundItems="true" OnSelectedIndexChanged="DDL_Country_SelectedIndexChanged">
                        </asp:dropdownlist>
                    </td>
                </tr>
            </contenttemplate>
            <triggers>
                <asp:asyncpostbacktrigger controlid="DDL_Country" />
            </triggers>
        </asp:updatepanel>
        <br />
        <asp:updatepanel id="StatePanel" runat="server" xmlns:asp="#unknown">
            <contenttemplate>
                <tr>
                    <td>
                        <asp:label id="lbl_State" runat="server" text="State" width="150px"></asp:label>
                    </td>
                    <asp:dropdownlist id="DDL_State" runat="server" width="150px" autopostback="true">
                        AppendDataBoundItems="true" OnSelectedIndexChanged="DDL_State_SelectedIndexChanged">
                    </asp:dropdownlist>
                    <td>
                    </td>
                </tr>
            </contenttemplate>
            <triggers>
                <asp:asyncpostbacktrigger controlid="DDL_State" />
            </triggers>
        </asp:updatepanel>
        <br />
        <asp:updatepanel id="CityPanel" runat="server" xmlns:asp="#unknown">
            <contenttemplate>
                <tr>
                    <td>
                        <asp:label id="lbl_City" runat="server" text="City" width="150px"></asp:label>
                    </td>
                    <td>
                        <asp:dropdownlist id="DDL_City" runat="server" width="150px" appenddatabounditems="true">
                            AutoPostBack="true">
                        </asp:dropdownlist>
                    </td>
                </tr>
            </contenttemplate>
            <triggers>
                <asp:asyncpostbacktrigger controlid="DDL_City" />
            </triggers>
        </asp:updatepanel>
    </div>
    </form>
</body>
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Jun-14 15:55pm    
How can it be an answer?
—SA

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