如果大类DropDownList1,有
AA
BB
CC
DD,
现在DropDownList1=BB;我点修改时显示效果如下。
BB
BB
CC
DD其中AA不见了,被BB取代了,怎么样才能正确的显示
BB
AA
BB
CC
DD我说的是点下拉框的时,里边显示的数据。

代码如下
 protected void show()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["webtest"].ToString();
        conn.Open();        string sqlss = "select * from Hover_Products where ID='" + Request["ID"]+"'";
        SqlCommand cmds = new SqlCommand(sqlss, conn);
        DataSet sets = new DataSet();
        SqlDataAdapter drs = new SqlDataAdapter();
        drs.SelectCommand = cmds;
        drs.Fill(sets);        if (sets.Tables[0].Rows.Count > 0)
        {
            this.title.Text = sets.Tables[0].Rows[0]["Product_Title"].ToString().Trim();
            this.p_ID.Text = sets.Tables[0].Rows[0]["Product_ID"].ToString().Trim();
            this.HtmlEditor1.Text = sets.Tables[0].Rows[0]["Product_News"].ToString().Trim();
            string c1= sets.Tables[0].Rows[0]["PClass"].ToString();
            string c2=sets.Tables[0].Rows[0]["PSmallClass"].ToString();
            string c3=sets.Tables[0].Rows[0]["PSmallClass1"].ToString();            SqlCommand cd = new SqlCommand();
            cd.Connection = conn;
            cd.CommandText = "select * from Hover_BigClassNews";
           
            DataSet se = new DataSet();
            SqlDataAdapter dr = new SqlDataAdapter();
            dr.SelectCommand = cd;
            dr.Fill(se);            if (se.Tables[0].Rows.Count > 0)
            {
                this.DropDownList1.DataSource=se;
                this.DropDownList1.DataTextField="BCNews_Name";
                this.DropDownList1.DataValueField="BCNews_ID";
                this.DropDownList1.AutoPostBack=true;
                this.DropDownList1.DataBind();                //if (se.Tables[0].Rows[0]["BCNews_Name"].ToString() == c1.Trim())
                //{
                    this.DropDownList1.SelectedItem.Text = c1.Trim();
                //}                SqlCommand cd1 = new SqlCommand();
                cd1.Connection = conn;
                cd1.CommandText = "select * from Hover_Small1";
               
                DataSet se1 = new DataSet();
                SqlDataAdapter dr1 = new SqlDataAdapter();
                dr1.SelectCommand = cd1;
                dr1.Fill(se1);                if (se1.Tables[0].Rows.Count > 0)
                {
                    this.DropDownList2.DataSource = se1;
                    this.DropDownList2.DataTextField = "Small1_Name";
                    this.DropDownList2.DataValueField = "Small1_ID";
                    this.DropDownList2.AutoPostBack = true;
                    this.DropDownList2.DataBind();
                    this.DropDownList2.SelectedItem.Text = c2.Trim();                    SqlCommand cd2 = new SqlCommand();
                    cd2.Connection = conn;
                    cd2.CommandText = "select * from Hover_Small2";                    DataSet se2 = new DataSet();
                    SqlDataAdapter dr2 = new SqlDataAdapter();
                    dr2.SelectCommand = cd2;
                    dr2.Fill(se2);                    if (se2.Tables[0].Rows.Count > 0)
                    {
                        this.DropDownList3.DataSource = se2;
                        this.DropDownList3.DataTextField = "Small2_Name";
                        this.DropDownList3.DataValueField = "Small2_ID";
                        this.DropDownList3.AutoPostBack = true;
                        this.DropDownList3.DataBind();
                        this.DropDownList3.SelectedItem.Text = c3.Trim();
                    }
                    else
                    {
                        this.DropDownList3.Enabled = false;
                        this.DropDownList3.AutoPostBack = false;
                        this.DropDownList3.SelectedItem.Text = "";
                    }                    dr2.Dispose();
                    dr2 = null;
                }
                else
                {
                    this.DropDownList2.Enabled = false;
                    this.DropDownList2.SelectedItem.Text = "";
                    this.DropDownList3.Enabled = false;
                    this.DropDownList3.SelectedItem.Text = "";
                }                dr1.Dispose();
                dr1 = null;
                    //this.DropDownList3.SelectedItem.Text=  c3.Trim();
                   
            }            dr.Dispose();
            dr = null;
        }        drs.Dispose();
        drs = null;
        conn.Close();
    }

解决方案 »

  1.   

    用foreach(ListItem li in DropDownList1.Items)
    {
        if(li.Text=="A")
          {
             li.Selected=true;
          }
    }
    或用索引
      

  2.   

          if (se.Tables[0].Rows[0]["BCNews_Name"].ToString() == c1.Trim())
                    {
                        this.DropDownList1.SelectedItem.Text = c1.Trim();
                    } 

    如果数据库中有这个值,就把把付给dropdownlist1,但是se.Tables[0].Rows[0]["BCNews_Name"].ToString()
    只取第一个值,怎样才能让它验索一下数据库中有没有这个数据