[英]dropdownlist selected index changed in asp.net using C#
I have 2 drop down list. 我有2个下拉列表。
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
.. 我设置了autopostback的city=true
..
Select is a function which is returning data table 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;
}
You didn't call DataBind()
after setting datasource. 设置数据源后,您没有调用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();
}
EDIT (with validator): 编辑(带有验证器):
ASPX: 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: 的.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. 该页面可与验证程序一起很好地工作。
尝试在您的下拉列表属性中设置Autopostback = true
you can use this code: 您可以使用以下代码:
foreach (ListItem item in YourDropDownList.Items)
{
if (item.Text == defaultText)
{
item.Selected = true;
break;
}
}
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. 我已经做了类似的案例,使用Category DropDownList和Product DropDownList来显示与所选类别相关的不同产品,并且每次用户选择类别时,列表都会更改。
First, make the AutoPostBack of the source (in my case Category, in your case City) to True. 首先,将源(在我的情况下为Category,在您的情况下为City)的AutoPostBack设置为True。
On the SelectedIndexChanged event: 在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();
}
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. 该代码应该能够显示状态列表,但是,如果仍然看不到状态,则添加代码以确保函数Select返回了数据。
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? 我认为您必须更新DropDown所在的更新面板...为什么不使用AJAX控件工具包级联DropDown? they are built for the same scenario as your. 它们是针对与您相同的场景而构建的。
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.