我的方法是这样写的
 private void CategoryAllMethod()
    {
        
        string departName = this.dplDepartList.SelectedItem.Text;
        Department depart = DepartmentManager.GetDepartIdByDepartName(departName);
        int departId = depart.DepartId;
        //Category cate = categoryManager.GetCategoryByDepartId(departId);
        
        IList<Category> list=categoryManager.GetCategoryListByDepartId(departId);
        this.dplCategoryList.DataSource = list;
        
            if (list.Count == 0)
            {
                // this.dplCategoryList.Item.Clear();
                this.dplCategoryList.SelectedItem.Text = "no categories";
            }
            else
            {
                this.dplCategoryList.DataValueField = "categoryId";
                this.dplCategoryList.DataTextField = "categoryName";
                this.dplCategoryList.DataBind();            }
    }protected void dplDepartList_SelectedIndexChanged(object sender, EventArgs e)
    {
               CategoryAllMethod();
   }根据第一个dropdownlist值的改变 而改变category的值
如果第一个dropdownlist值里面没有对应的category,我就让category的dropdownlist显示为no categories但是现在的问题是 我通过上述方法 category的dropdownlist的值第一个的确显示的是“no categories”。
但是下面还有初次绑定的时候的那些值。说明category的dropdownlist没有清空。
然后我用this.dplCategoryList.Item.Clear()方法清空,就是我上面注释的那行代码,
运行到this.dplCategoryList.Item.Clear()下一行的时候 就会报  未将对象引用到设置的实例
无论我把那行代码放到if里面 还是外面 都会报这个错请各位帮忙解决一下。谢谢你们了。

解决方案 »

  1.   

    this.dplCategoryList.Items.Clear();
      

  2.   

    你都把里面的项清空了
    在用this.dplCategoryList.SelectedItem.Text肯定报错了
      

  3.   

    然后我用this.dplCategoryList.Item.Clear()方法清空,就是我上面注释的那行代码,
    运行到this.dplCategoryList.Item.Clear()下一行的时候 就会报 未将对象引用到设置的实例
    无论我把那行代码放到if里面 还是外面 都会报这个错
      

  4.   

    运行到this.dplCategoryList.Item.Clear()下一行的时候 就会报 未将对象引用到设置的实例
      

  5.   

    this.dplCategoryList.SelectedItem.Text = "no categories";
    这句改成:
    this.dplCategoryList.Items.Add( "no categories");
    this.dplCategoryList..SelectedValue= "no categories";
      

  6.   


    是不要上面我注释的那行代码? 我试过了。这样写的话。之前加载时候的category也会显示在no category 下面
      

  7.   

     if (list.Count == 0)
      {
      this.dplCategoryList.Item.Clear();
      this.dplCategoryList.Item.add(new ListItem("no categories","0"));
      }
      

  8.   

    AddItem() 根本就点不出来阿
      

  9.   

    if (list.Count == 0)
      {
      this.dplCategoryList.Items.Clear();
      ListItem item = new ListItem();
            item.Value = "-1";
            item.Text = "no categories";
            dplCategoryList.Items.Add(item);
      }
    else
      {
      this.dplCategoryList.DataValueField = "categoryId";
      this.dplCategoryList.DataTextField = "categoryName";
      this.dplCategoryList.DataBind();  }
      

  10.   

     this.dplCategoryList.Items.Clear();
                    this.dplCategoryList.Items.Add("No categories");我是这样写的 出来了 问题解决了 谢谢你们每一个人!