dropdownlist选择值的奇怪行为

时间:2012-08-26 17:00:19

标签: 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, "--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--");

        }

1 个答案:

答案 0 :(得分:0)

这是正常的,因为你每次重新绑定你的DDL,你必须只有一次whane

if(! IsPostBack)
{
   //Just one time here

}

您使用Viewstate保留DDL

您可以在DDL上添加Chandra AutoPostBack="true",并在DDL上添加OnSelectChanged

在您的数据库中,验证您的州是否已映射到国家/地区的良好名称

例如DropDownList1.SelectedItem.Text ==&#34; USA&#34;

var query = (ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));

验证状态是否已映射到名称,即美国