DropDownList属性不是很懂,请大家解释下以下代码:
DropDownList1.DataSource=DataSet1.Tables["ClassName"].DefaultView;
DropDownList1.DataTextField="className";
DropDownList1.DataValueField="className";
DropDownList1.DataBind(); 
DropDownList1.Items.Add("全部");
DropDownList1.Items.FindByText("全部").Selected=true;
ListItem d=new ListItem(classname,classname);
DropDownList1.Items.Add(d);

解决方案 »

  1.   

    DropDownList1.DataSource=DataSet1.Tables["ClassName"].DefaultView; //这里表示对数据源赋值
    DropDownList1.DataTextField="className"; //表示设置显示字段名:className
    DropDownList1.DataValueField="className"; //表示设置值字段:className
    DropDownList1.DataBind();  //绑定数据
    DropDownList1.Items.Add("全部"); //下拉框再加一行为“全部”的数据。
    DropDownList1.Items.FindByText("全部").Selected=true; //默认选择全部
      

  2.   

    //设置DropDownList1的数据源
    DropDownList1.DataSource=DataSet1.Tables["ClassName"].DefaultView; 
    //设置绑定的文本字段,简单的说,这个字段是用来给用户看的
    DropDownList1.DataTextField="className"; 
    //设置绑定的值字段,简单的说,这个字段是用来给开发人员用的,大多情况下应绑定主键
    DropDownList1.DataValueField="className"; 
    //明确绑定数据
    DropDownList1.DataBind();  
    //添加一项,这步不如替换成DropDownList1.Items.Insert(0, new ListItem("全部","全部")); 
    DropDownList1.Items.Add("全部"); 
    //根据"全部"这个字符串找到DropDownList的这项并设置其为选中
    DropDownList1.Items.FindByText("全部").Selected=true; //下面这两步貌似无意义
    ListItem d=new ListItem(classname,classname); 
    DropDownList1.Items.Add(d);