1

I have 2 drop down list.

  • First for city and
  • second for state

I want to fill the state list when the city is selected...

I'm using the code

protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
    {

        String sqlQuery="select SM.StateId,StateName From StateMast SM,CityMast CM where CM.StateId=SM.StateId AND CM.CItyId='"+ Convert.ToInt16(DdlCity.SelectedValue.ToString())+"'";

        DdlState.DataSource = cls.Select(sqlQuery);
        DdlState.DataTextField = "StateName";
        DdlState.DataValueField = "StateId";
    } 

but nothing is happing on selecting city...

I have set the autopostback of city=true..

Select is a function which is returning data table

public DataTable Select(String sqlQuery)
   {       
       con.Open();
       SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery,con);
       DataTable table = new DataTable();
       adapter.Fill(table);
       con.Close();
       return table;
   }

5 Answers 5

2

You didn't call DataBind() after setting datasource.

protected void Ddl_SelectedIndexChanged(object sender, EventArgs e) {
    String sqlQuery="select SM.StateId,StateName From StateMast SM,CityMast CM where CM.StateId=SM.StateId AND CM.CItyId='"+ Convert.ToInt16(DdlCity.SelectedValue.ToString())+"'";

    DdlState.DataSource = cls.Select(sqlQuery);
    DdlState.DataTextField = "StateName";
    DdlState.DataValueField = "StateId";
    DdlState.DataBind();
} 



EDIT (with validator):
ASPX:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" 
            onselectedindexchanged="ddlCity_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="rfvCity" runat="server" ErrorMessage="City is required" ControlToValidate="ddlCity" InitialValue="0" Display="Dynamic"></asp:RequiredFieldValidator>
        <br />
        <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>


.cs:

public partial class ChildDDL : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
            return;

        ddlCity.Items.Add(new ListItem("Select One", "0"));
        ddlCity.Items.Add(new ListItem("City 1", "1"));
        ddlCity.Items.Add(new ListItem("City 2", "2"));
        ddlCity.Items.Add(new ListItem("City 3", "3"));

        List<State> lstState = new List<State>();
        lstState.Add(new State() { StateID = 1, StateName = "State 1", CityID = 1 });
        lstState.Add(new State() { StateID = 2, StateName = "State 2", CityID = 1 });
        lstState.Add(new State() { StateID = 3, StateName = "State 3", CityID = 1 });
        lstState.Add(new State() { StateID = 4, StateName = "State 4", CityID = 2 });
        lstState.Add(new State() { StateID = 5, StateName = "State 5", CityID = 2 });
        lstState.Add(new State() { StateID = 6, StateName = "State 6", CityID = 2 });
        lstState.Add(new State() { StateID = 7, StateName = "State 7", CityID = 3 });
        lstState.Add(new State() { StateID = 8, StateName = "State 8", CityID = 3 });

        Session["lstState"] = lstState;
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<State> lstState = (List<State>)Session["lstState"];

        ddlState.DataSource = lstState
            .Where(state => state.CityID == Convert.ToInt32(ddlCity.SelectedValue)); ;
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateID";
        ddlState.DataBind();
    }

    public class State
    {
        public int StateID { get; set; }
        public string StateName { get; set; }
        public int CityID { get; set; }
    }
}

The page works well with validator.

Sign up to request clarification or add additional context in comments.

4 Comments

yaa i forgot... but now i added it... but its not working... what should i do?????????
the problem is that there are validators... so the function is not working.... means no post back occuring.. and no selected index change function call.....
this is personal information form... and i am using spry validatos.. i have created them in dreamweawer
I have modified my code with validator added. This is the best I can do.
1

try to set Autopostback = true in your dropdownlist properties

1 Comment

an overlooked attribute most of the time. even tho this may not have solved the OP, this is a very important thing to note
0

I think you have to update the update Panel in which the DropDowns are located...why aren't you using the AJAX Controls Toolkit Cascading DropDown? they are built for the same scenario as your.

http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx

7 Comments

is there no way to do this without ajax
you can do a complete PostBack but its considered a bad practice.
how can i do complete post back??????? i have to submit my project on 2 o clock... so i have to do it with complete post back now... i will try ajax later
what type of validation are you performing?is it related to DropDown?
For testing purpose disable the validators & then check whether the event is being fired or not.
|
0

you can use this code:

foreach (ListItem item in YourDropDownList.Items)
 {
        if (item.Text == defaultText) 
        {
            item.Selected = true;
            break;
        }
 }

Comments

0

I have done similar case, with a Category DropDownList and Product DropDownList to show different product related to the selected category, and the list will change every time user select a category.

First, make the AutoPostBack of the source (in my case Category, in your case City) to True.

On the SelectedIndexChanged event:

if ( DdlCity.SelectedValue != null ) {
    String sqlQuery="select SM.StateId,StateName From StateMast SM,CityMast CM where CM.StateId=SM.StateId AND CM.CItyId='"+ Convert.ToInt16(DdlCity.SelectedValue.ToString())+"'";

    // Try to get the DataTable first
    DataTable data = cls.Select(sqlQuery);

    // I assume there is a Label name lblNumOfData
    lblNumOfData.Text = String.Format("There is {0} state", data.Rows.Count);

    DdlState.DataSource = data;
    DdlState.DataValueField = "StateId";
    DdlState.DataTextField = "StateName";
    DdlState.DataBind();
}

This code should be able to show the state list, however, if you still cannot see the state, I add the code to make sure that there is data returned by the function Select.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.