Reputation: 128
I have 2 drop down list.
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;
}
Upvotes: 1
Views: 23616
Reputation: 360
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.
Upvotes: 0
Reputation: 31
try to set Autopostback = true in your dropdownlist properties
Upvotes: 1
Reputation: 453
you can use this code:
foreach (ListItem item in YourDropDownList.Items)
{
if (item.Text == defaultText)
{
item.Selected = true;
break;
}
}
Upvotes: 0
Reputation: 5480
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.
Upvotes: 2
Reputation: 1410
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
Upvotes: 0