tags:

views:

30

answers:

2

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...

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;

}

+2  A: 

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.

Lee Sy En
yaa i forgot... but now i added it... but its not working... what should i do?????????
Sheetal Inani
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.....
Sheetal Inani
what validator is that?
Lee Sy En
this is personal information form... and i am using spry validatos.. i have created them in dreamweawer
Sheetal Inani
I have modified my code with validator added. This is the best I can do.
Lee Sy En
ok... thank you.. so much
Sheetal Inani
A: 

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

KhanZeeshan
is there no way to do this without ajax
Sheetal Inani
you can do a complete PostBack but its considered a bad practice.
KhanZeeshan
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
Sheetal Inani
is function getting called?
KhanZeeshan
no.... beacouse of validators the function is not caling
Sheetal Inani
what should i do?????????
Sheetal Inani
what type of validation are you performing?is it related to DropDown?
KhanZeeshan
KhanZeeshan
this is personal information form... and i am using spry validatos.. i have created them in dreamweawer... the validators are not related to dropdown list... but they are on the page....
Sheetal Inani
its working without the validators
Sheetal Inani
i'll suggest you use ASP.NET's own validators.
KhanZeeshan
ok.... thank you
Sheetal Inani