怎么后台添加CheckBoxList并且得到选择结果

发表于:2007-06-30来源:作者:点击数: 标签:
private void Page_Load(object sender, System.EventArgs e) { CheckBoxList cbl=new CheckBoxList(); cbl.ID="cbl"; cbl.Items.Add(new ListItem("1","1")); cbl.Items.Add(new ListItem("2","2")); cbl.Items.Add(new ListItem("3","3")); this.Page.Cont

private void Page_Load(object sender, System.EventArgs e)
  {
   CheckBoxList cbl=new CheckBoxList();
   cbl.ID="cbl";
   cbl.Items.Add(new ListItem("1","1"));
   cbl.Items.Add(new ListItem("2","2"));
   cbl.Items.Add(new ListItem("3","3"));
   this.Page.Controls[1].Controls.Add(cbl);
   cbl.SelectedIndexChanged+= new System.EventHandler(this.CheckBoxList1_SelectedIndexChanged);
   cbl.AutoPostBack=true;
  }

private void CheckBoxList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   Response.Write("选中项的值:<br>");
   foreach(ListItem li in ((CheckBoxList)sender).Items)
   {
    if(li.Selected)
    {
     Response.Write(li.Value+"<br>");
    }
   }
  }

原文转自:http://www.ltesting.net