Auiso一二空间

DropDownList控件使用

DropDownList控件与RadioButtonList控件相似。DropDownList表示一组相互排斥的选项。可以通过三种方式添加选项到DropDownList:在声明DropDownList时列出选项(即在HTML代码中或借助属性编辑器添加);直接往DropDownList控件的ListItemCollection集合中添加项(如下例1、列2);绑定DropDownList到数据源(例3)。
例1:
  private void Page_Load(object sender, System.EventArgs e)
  {   this.DropDownList1.Items.Add("一");
   Response.Write(this.DropDownList1.SelectedValue);//等同于this.DropDownList1.SelectedItem.Value
  }//此时Value属性同步与Text属性 显示结果为:一  ;SelectedIndex值为0;
例2:protected System.Web.UI.WebControls.DropDownList DropDownList1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {   ListItem li =new ListItem();
   li.Text="一";
   li.Value="1";  
   this.DropDownList1.Items.Add(li);//或可以采取 this.DropDownList1.Items.Add(new  ListItem("一","1"))
   Response.Write(this.DropDownList1.SelectedItem.Value);
}/此时Value属性为1,Text属性为一 显示结果为:1  ;SelectedIndex值为0;

例3:
protected System.Web.UI.WebControls.DropDownList ddlShengOld;
  string strSql="select sheng,shengid from t_sheng";
   SqlDataReader sdr=Auiso.Base.ExecuteSql_sdr(strSql);
   this.ddlShengOld.DataSource=sdr;
   this.ddlShengOld.DataTextField="sheng";//对应Text
   this.ddlShengOld.DataValueField="shengid";//对应Value  不显示出来
   this.ddlShengOld.DataBind();
例4:初始化选定某个值的方法
foreach (ListItem li in this.ddlist)
   {
    if (li.Value=="设定值")
    {
     li.Selected=true;
    }
   }
或使用DropDownList1.SelectedIndex   =   DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(str1))

例5:实现联动
在该控件的CheckedChanged事件编程,并将AutoPostBack属性设置为True
例6:实现ListBox或DropDownList之间项目转移 
用this.ListBox1.SelectedIndex=-1;
   this.ListBox1.Items.Add(this.DropDownList1.SelectedItem);
   this.DropDownList1.Items.Remove(this.DropDownList1.SelectedItem);
ListBox 可以通过ListSelectionMode 设置多选,默认值为 Single

posted on 2006-09-02 10:14  阿社  阅读(1594)  评论(1编辑  收藏  举报

导航