0

I am making a Webpage in asp.net which is having three dropdown lists
One for Country, Second for State & Third for City.

Here's the code for dropdown list:

<tr>
<td class="td1">&nbsp;&nbsp; Country: </td>
<td>
    <asp:DropDownList ID="ddCountry" runat="server" Width="250px" 
        onselectedindexchanged="ddCountry_SelectedIndexChanged">
        <asp:ListItem>India</asp:ListItem>
        <asp:ListItem>UK</asp:ListItem>
        <asp:ListItem>USA</asp:ListItem>
    </asp:DropDownList><br />
    <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
    </td>
</tr>

<tr>
<td class="td1">&nbsp;&nbsp; State: </td>
<td>
    <asp:DropDownList ID="ddState" runat="server" Width="250px" 
        onselectedindexchanged="ddState_SelectedIndexChanged"></asp:DropDownList><br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
        ErrorMessage="Please select your state" ControlToValidate="ddState"></asp:RequiredFieldValidator>
    </td>
</tr>

<tr>
<td class="td1">&nbsp;&nbsp; City: </td>
<td>
    <asp:DropDownList ID="ddCity" runat="server" Width="250px"></asp:DropDownList><br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
        ErrorMessage="Please select your city" ControlToValidate="ddCity"></asp:RequiredFieldValidator>
    </td>
</tr>

This is the code for Registration.aspx.cs page

protected void ddCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddCountry.SelectedIndex==1)
        {
            ddState.Items.Clear();
            ddState.Items.Add("Delhi");
            ddState.Items.Add("Karnataka");
            ddState.Items.Add("Maharashtra");
            ddState.Items.Add("West Bengal");
        }

        else if (ddCountry.SelectedIndex==2)
        {
            ddState.Items.Clear();
            ddState.Items.Add("England");
            ddState.Items.Add("Scotland");
            ddState.Items.Add("Wales");
        }

        else
        {
            ddState.Items.Clear();
            ddState.Items.Add("California");
            ddState.Items.Add("Nevada");
            ddState.Items.Add("New York");
        }

    }
    protected void ddState_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddState.SelectedValue.Equals("California"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Alameda");
            ddCity.Items.Add("Los Angeles");
            ddCity.Items.Add("Sacramento");
        }

        else if (ddState.SelectedValue.Equals("Delhi"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Agra");
            ddCity.Items.Add("New Delhi");
            ddCity.Items.Add("Siri");
        }

        else if (ddState.SelectedValue.Equals("England"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Durham");
            ddCity.Items.Add("Liverpool");
            ddCity.Items.Add("Manchester");
            ddCity.Items.Add("Winchester");
        }

        else if (ddState.SelectedValue.Equals("Karnataka"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Belgaon");
            ddCity.Items.Add("Bengalore");
            ddCity.Items.Add("Gulbarga");
            ddCity.Items.Add("Mysore");
        }

        else if (ddState.SelectedValue.Equals("Maharashtra"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Amaravati");
            ddCity.Items.Add("Kolhapur");
            ddCity.Items.Add("Mumbai");
            ddCity.Items.Add("Nagpur");
            ddCity.Items.Add("Pune");
            ddCity.Items.Add("Ratnagiri");
            ddCity.Items.Add("Solapur");
            ddCity.Items.Add("Thane");
        }

        else if (ddState.SelectedValue.Equals("Nevada"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Eureka");
            ddCity.Items.Add("Gold Field");
            ddCity.Items.Add("Las Vegas");
            ddCity.Items.Add("Virginia City");
        }

        else if (ddState.SelectedValue.Equals("New York"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Albany");
            ddCity.Items.Add("Lake Placid");
            ddCity.Items.Add("New York City");
            ddCity.Items.Add("Syracuse");
        }

        else if (ddState.SelectedValue.Equals("Scotland"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Edinburgh");
            ddCity.Items.Add("Glasgow");
            ddCity.Items.Add("Perth");
        }

        else if (ddState.SelectedValue.Equals("Wales"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Cardiff");
            ddCity.Items.Add("Newport");
            ddCity.Items.Add("Swansea");
        }

        else if (ddState.SelectedValue.Equals("West Bengal"))
        {
            ddCity.Items.Clear();
            ddCity.Items.Add("Darjeeling");
            ddCity.Items.Add("Hooghly");
            ddCity.Items.Add("Howrah");
            ddCity.Items.Add("Kolkata");
        }
    }

The code is not working properly.
Have I done anything wrong? or is there some mistake in my code?
After selecting any value in Country the State is not filling up with proper values.
It only keeps itself blank.

What should I do now??

2
Contributors
2
Replies
13
Views
4 Years
Discussion Span
Last Post by vinnitro
1

Well Vinod Ji You have forgot to add autopostback="true" in your dropdown selectedindexchanged event..
This would not allow your country dropdown to postback and make a roundtrip down the server..
So Your State Dropdown is not getting populated..
Modify Your codes as follows:

<tr>
<td class="td1">&nbsp;&nbsp; Country: </td>
<td>
<asp:DropDownList ID="ddCountry" runat="server" Width="250px" autopostback="true"
onselectedindexchanged="ddCountry_SelectedIndexChanged">
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
</asp:DropDownList><br />
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="td1">&nbsp;&nbsp; State: </td>
<td>
<asp:DropDownList ID="ddState" runat="server" Width="250px" autopostback="true"
onselectedindexchanged="ddState_SelectedIndexChanged"></asp:DropDownList><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ErrorMessage="Please select your state" ControlToValidate="ddState"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="td1">&nbsp;&nbsp; City: </td>
<td>
<asp:DropDownList ID="ddCity" runat="server" Width="250px"></asp:DropDownList><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ErrorMessage="Please select your city" ControlToValidate="ddCity"></asp:RequiredFieldValidator>
</td>
</tr>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.