我有2个下拉列表。
我想在选择城市时填写州名单......
我正在使用代码
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";
}
但没有什么可以选择城市......
我设置了city=true
..
Select是一个返回数据表的函数
public DataTable Select(String sqlQuery)
{
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery,con);
DataTable table = new DataTable();
adapter.Fill(table);
con.Close();
return table;
}
答案 0 :(得分:2)
设置数据源后没有调用DataBind()
。
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();
}
编辑(带验证器):
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; }
}
}
该页面适用于验证器。
答案 1 :(得分:1)
尝试在下拉列表属性中设置Autopostback = true
答案 2 :(得分:0)
我认为你必须更新DropDowns所在的更新面板......你为什么不使用AJAX Controls Toolkit Cascading DropDown?它们的构建方式与您的相同。
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx
答案 3 :(得分:0)
您可以使用此代码:
foreach (ListItem item in YourDropDownList.Items)
{
if (item.Text == defaultText)
{
item.Selected = true;
break;
}
}
答案 4 :(得分:0)
我做了类似的情况,使用Category DropDownList和Product DropDownList来显示与所选类别相关的不同产品,每当用户选择一个类别时,列表就会改变。
首先,将源的AutoPostBack(在我的情况下为Category,在您的案例中为City)设为True。
在SelectedIndexChanged事件中:
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();
}
此代码应该能够显示状态列表,但是,如果仍然看不到状态,我会添加代码以确保函数Select返回数据。