我的CheckBoxList是在repeater的ItemDataBound事件里面动态生成的,现在在按钮的添加事件下面应该如何获取CheckBoxList选中的值呢?求助!

解决方案 »

  1.   

    贴点源码上来看看吧,应该用FindControl方法可以找到的
      

  2.   

    遍历repeater
    FindControl().
    Checkboxlist chkblist=(Checkboxlist)Repeater.Item.FindControl(""); foreach (ListItem items in CheckBoxList1.Items
    {
        items.Selected = true;
        //...
    }
      

  3.   

    以前写的一个 。你看下有用么
     for (int i = 0; i < Repeater1.Items.Count; i++)
                    {
                        Repeater repeater2 = Repeater1.Items[i].FindControl("Repeater2") as Repeater;
                        if (repeater2 != null)
                        {
                            for (int j = 0; j < repeater2.Items.Count; j++)
                            {
                                CheckBox cb = repeater2.Items[j].FindControl("chkbox2") as CheckBox;
                                cb.Checked = false;
                                Label lbnum = repeater2.Items[j].FindControl("Label1") as Label;
                                for (int h = 0; h < ssid.Length - 1; h++)
                                {
                                    if (lbnum.Text == ssid[h].ToString())
                                    {
                                        cb.Checked = true;
                                    }
                                }                        }
                        }
                    }
      

  4.   

    protected void RepList_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {  Label lbltype = e.Item.FindControl("lblType") as Label;
                    Label lblID = e.Item.FindControl("lblID") as Label;
                    Panel pan = e.Item.FindControl("pan") as Panel;
                    type =Convert.ToInt32(lbltype.Text);
                    DataSet ds = queBll.GetList(0, " id=" + lblID.Text + " ", "");
                    string[] d = ds.Tables[0].Rows[0]["question"].ToString().Replace("\r\n", "\r").Split(new char[] { '\r' });                  
                    if (type == 28)//找到repeater里面的RadioButtonList  单选
                    {
                        RadioButtonList rndanxuan = new RadioButtonList();
                        rndanxuan.ID = "rndanxuan";
                        rndanxuan.DataSource = d;
                        rndanxuan.DataBind();
                        pan.Controls.Add(rndanxuan);
                    }
                    if (type == 29)
                    {
                        RadioButtonList rbdanxuanwenben = new RadioButtonList();
                        rbdanxuanwenben.ID = "rbdanxuanwenben";
                        rbdanxuanwenben.DataSource = d;
                        rbdanxuanwenben.DataBind();
                        pan.Controls.Add(rbdanxuanwenben);
                    }
                    if (type == 30)
                    {
                        CheckBoxList chkduo = new CheckBoxList();
                        chkduo.ID = "chkduo";
                        chkduo.DataSource = d;
                        chkduo.DataBind();
                        pan.Controls.Add(chkduo);
                    }
    }
    }//然后我要在按钮的添加事件下面获取这些选中的值
     protected void bnt_add_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < RepList.Items.Count; i++)
                {
                    RadioButton rbl = RepList.Items[i].FindControl("rbdanxuanwenben") as RadioButton;
                    if (rbl != null)
                    {
                        //记录选中的答案                    if (rbl.Checked)
                        {
                           
                        }
                    }
                }
            }
    我应该怎么取值呢
      

  5.   

    目测是 RepList.Items[i].FindControl==null因为你加了Panel soRadioButton rbl = (RepList.Items[i].FindControl("pan") as pannel).FindControl("cb") as RadioButton ;