我正在用另一个 DDL 填充一个 DDL,并且我正在从另一个页面获取值`
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,比如英国或美国,DropDownList1.SelectedItem.Text 值仍然是南非(会话中的值),如果我选择南非以上的国家,DDL 正在填充我不它背后的原因是什么?有什么解决方案或建议吗?
f (!IsPostBack)
{
DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "--Select a country--");
}