C# dropdownlist所选值的奇怪行为
我用另一个DDL填充一个DDL,并从另一个页面获取值`C# dropdownlist所选值的奇怪行为,c#,asp.net,C#,Asp.net,我用另一个DDL填充一个DDL,并从另一个页面获取值` protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.DataSource = ProfileMasterDAL.bindcountry(); DropDownList1.DataBind(); DropDownList1.Items.Insert(0, "
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "--Select country--");
}
if(Session["uname"]!=null)
{
DropDownList1.SelectedValue = Session["country"].ToString();
ProfileMasterBLL bll=new ProfileMasterBLL();
foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
{
if (VARIABLE.ToString().Contains(DropDownList1.SelectedItem.Text))
{
var query = (ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));
DropDownList2.DataSource = query;
DropDownList2.DataBind();
}
}
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(DropDownList1.SelectedItem.Text));
DropDownList1.Items.FindByText(DropDownList1.SelectedItem.Text).Selected = true;
string str = DropDownList1.SelectedItem.Value;
DropDownList1.Items.FindByText(DropDownList1.SelectedItem.Text).Selected = true;
var states = (new ProfileMasterDAL()).GetStatesByCountry(str);
DropDownList2.Enabled = true;
DropDownList2.Items.Clear();
DropDownList2.DataSource = states;
DropDownList2.DataBind();
}
<countrys>
<country>
<name>India</name>
<state>
<text>Maharashtra</text>
<text>Kashmir</text>
<text>Goa</text>
</state>
</country>
<country>
<name>Sri lanka</name>
<state>
<text>Kanady</text>
<text>Colombo</text>
<text>Galle</text>
</state>
</country>
<country>
<name> Australia</name>
<state>
<text>Sydney</text>
<text>Perth</text>
<text>Melbourne</text>
</state>
</country>
<country>
<name>South Africa</name>
<state>
<text>Capetown</text>
<text>Johanusburg</text>
<text>Durban</text>
</state>
</country>
<country>
<name>USA</name>
<state>
<text>Alabama</text>
<text>California</text>
<text>Detroit</text>
</state>
</country>
<country>
<name>UK</name>
<state>
<text>London</text>
<text>Paris</text>
<text>Italy</text>
</state>
</country>
</countrys>
这很正常,因为每次重新绑定DDL时,必须只绑定一次
if(! IsPostBack)
{
//Just one time here
}
您可以使用Viewstate持久化DDL
您可以在DDL上添加ChandraAutoPostBack=“true”
,并在DDL上添加OnSelectChanged
在您的数据库中,验证您的州是否映射到国家的良好名称
例如DropDownList1.SelectedItem.Text==“美国”
验证州是否映射到好名字,谁是美国我只在if(!ispotback)中绑定我的国家一次如果是这样,那么即使其他国家也不会填充他们的州对吗??更新了我的问题检查一次..Chandra你可以在你的DDL上添加AutoPostBack=“true”,即使它不起作用,因为我可以填充一些州,而我无法填充其他州,这很奇怪…好吧,我了解你的位置没有填充的国家列表是什么?
if(! IsPostBack)
{
//Just one time here
}
var query = (ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));