页面上有一个DropDownList1
protected void Page_Load(object sender, EventArgs e)
    {        if (!IsPostBack)
        {
            //提取数据库记录的部门字段邦定到DropDownList1
            DepartmentBLL db = new DepartmentBLL();
            db.GetDptnameToDropDown(user_dpt);
        }
}如有:工程部
货仓部
品质部
人事部
业务部
...我用如下方法赋值都失败:
1、DropDownList1.SelectedValue = "品质部";     错误提示:DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中。参数名: value 
2、DropDownList1.SelectedItem.Text  = "品质部";   程序运行通过,但是却把第一项"人事部"无凭无据换为品质部了,那我的列表就被改了:
品质部
货仓部
品质部
人事部
业务部
...3、DropDownList1.Items.Insert(0, "品质部");     程序运行通过,但是却是在列表最前面加多一项"品质部"。
品质部
工程部
货仓部
品质部
人事部
业务部
...
请问如何赋值才能使列表自动定位到那一项??
(注:一定要用代码邦定数据,不是在设计阶段就设定列表的各项)

解决方案 »

  1.   

    是不是你绑定的时候没有区分value和Text的对应字段?DropDownList1.DataSource=dt;
    DropDownList1.DataValueField="值字段";
    DropDownList1.DataTextField="显示字段";
    DropDownList1.DataBind();//再默认你要选择的值。
    DropDownList1.SelectedValue = "品质部";
      

  2.   

     DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(质部'));
      

  3.   

    赋的值应该在DropDownList的DataSource中DropDownList1.SelectedIndex = 1;
    or
    DropDownList1.Text = "";//try
      

  4.   

    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText('品质部'));
      

  5.   

    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("品质部"));
      

  6.   

    DropDownList1”有一个无效 SelectedValue 的原因是因为你的 SelectedValue 中没有"品质部"这个值啊你应该查出"品质部" 对应的Value 用哪个value去定位this.DropDownList1.SelectedValue = "品质部---Value";或者:
                foreach (ListItem li in DropDownList1.Items)
                {
                    if (li.Text == "品质部")
                    {
                        li.Selected = true;
                    } 
                } 
      

  7.   

    可以写个通用函数的,DropDownList和CheckBoxList之类的都可以用:public void BindListControl(ListControl lstControl, object dtSource, string textField, string valueField, string selValue) 
    {
         lstControl.DataTextField = textField;
         lstControl.DataValueField = valueField;
         lstControl.DataSource = dtSource;
         lstControl.DataBind();     if (!string.IsNullOrEmpty(selValue)) 
         {
              lstControl.SelectedValue = selValue;
         }
    }
      

  8.   

    ListItem li=DropDownList1.Items.FindByVaue(""); 
     if(li!=null)li.Seleted=true;
      

  9.   

    DataTable dt = db.GetDptnameToDropDown(user_dpt);//
    DropDownList1.DataSource = dt;
    DropDownList1.DataValueField="DptIDs";
    DropDownList1.DataTextField="Dptname";
    DropDownList1.DataBind();
    DropDownList1.SelectedValue = "品质部";或者
    //
    DataTable dt = db.GetDptnameToDropDown(user_dpt);for(int i = 0 ;i<dt.Rows.Count;i++)
    {
        ListItem item = new ListItem();
        item.Text = dt.Rows[i]["Dptname"].ToString();
        item.Value = dt.Rows[i]["DptID"].ToString();
        if(item.Text == "品质部")
        {
            item.Selected = true;
        }
        DropDownList1.Items.Add(item);
    }
      

  10.   

    是我搞错了,邦定数据时没有去掉空格,
    select dpt_name,dpt_code from tb_department order by dpt_name改为如下就好了
    DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommond("select rtrim(dpt_name) as dpt_name,rtrim(dpt_code) as dpt_code from tb_department order by dpt_name");
            ddl.DataSource = db.ExecuteDataTable(cmd);
            ddl.DataValueField = "dpt_code";
            ddl.DataTextField = "dpt_name";
            ddl.DataBind();请谢各位,你们的方法对我很有启发,谢谢!!
      

  11.   

    無效 SelectedValue, 請確認數據庫是否有數值可以綁定