我在repeater中嵌套了一个checkboxlist控件,现在我要取得checkboxlist的选中值,该如何实现,大家帮忙看看。非常感谢。相关代码:前台:
<asp:Repeater ID="repposition" runat="server" 
                    onitemdatabound="repposition_ItemDataBound">
                    <ItemTemplate>
                        <asp:HiddenField ID="hidshow" runat="server" Value='<%#Eval("showID") %>' />
                        <span style="border: 0px solid #325FB5; background-color: #F1F6FC; font-size:16px; font-weight:bold;">
                            <%#Eval("showExplain")%></span>
                        <asp:CheckBoxList ID="chkPostion" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" AutoPostBack="true"
                            Width="623px">
                        </asp:CheckBoxList>
                    </ItemTemplate>
                </asp:Repeater>
后台:private void BindFpoistion()
    { 
        WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
        repposition.DataSource = bll_sp.GetList(" fatherID=0");
        repposition.DataBind();
    }
protected void repposition_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
        CheckBoxList cbl;
        int id;
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            cbl = (CheckBoxList)e.Item.FindControl("chkPostion");
            id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidshow")).Value);
            cbl.DataSource = bll_sp.GetList(" fatherID=" + id);
            cbl.DataValueField = "showID";
            cbl.DataTextField = "showExplain";
            cbl.DataBind();
            string postion = "";
            for (int y = 0; y < cbl.Items.Count; y++)
            {
                if (cbl.Items[y].Selected)
                {
                    postion += cbl.Items[y].Value + ",";
                }
            }
            ViewState["position"] = postion; 
          
        }
    }    红色部分是我尝试弄的,不好使,大家看一下,推荐一下实现方法。非常感谢!

解决方案 »

  1.   

    用两个repeater 设置它们之间的父子关系tableRelation 还有它们联系的主键
      

  2.   

    ItemDataBound 事件是列表绑定时执行的吧?
    你想它什么时候取值哇?
      

  3.   

    不应该再ItemDataBound事件中取值,因为重新绑定后,原来的选中状态都会被清除掉。你可以在其它回调处理事件中取值,要在重新绑定之前,可以用下面的语句来找到CheckBoxList:
    CheckBoxList cbl = (CheckBoxList)repposition.Items[index].FindControl("chkPostion");
    index是你要找的第n行,找到后就可以通过循环来判断是否Selected
      

  4.   

    昨晚弄了一下,实现了部分效果还是有些问题;我修改后的代码:
    前台:
    <asp:Repeater ID="repposition" runat="server" 
                        onitemdatabound="repposition_ItemDataBound" >
                        <ItemTemplate>
                            <asp:HiddenField ID="hidshow" runat="server" Value='<%#Eval("showID") %>' />
                            <span style="border: 0px solid #325FB5; background-color: #F1F6FC; font-size:16px; font-weight:bold;">
                                <%#Eval("showExplain")%></span>                             
                            <asp:CheckBoxList ID="chkPostion" runat="server" OnSelectedIndexChanged="chkSelectJudge" RepeatColumns="4" RepeatDirection="Horizontal"
                                Width="623px">
                            </asp:CheckBoxList>                       
                        </ItemTemplate>
                    </asp:Repeater>  
    后台:   
     protected void repposition_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
            CheckBoxList cbl;
            int id;
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                cbl = (CheckBoxList)e.Item.FindControl("chkPostion");
                id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidshow")).Value);
                cbl.DataSource = bll_sp.GetList(" fatherID=" + id);
                cbl.DataValueField = "showID";
                cbl.DataTextField = "showExplain";
                cbl.DataBind();
            }
        }
        protected void chkSelectJudge(object sender, EventArgs e)
        {
            CheckBoxList chkl; //= new CheckBoxList();
            chkl = (CheckBoxList)sender;
            string postion = "";
            for (int i = 0; i < chkl.Items.Count; i++)
            {
                if (chkl.Items[i].Selected)
                {
                    postion += chkl.Items[i].Value + ",";
                }
            }
            ViewState["position"] = postion;
        }
    蓝色部分是我在checkboxlist中加了个事件,然后在后台(红色部分)编写这个事件的相应方法,测试后的结果是,只能取到最后一级的子级复选框,之前的一级下的复选框取不到值,调试一下结果发现第一次进来时,chkl.Items.Count的大小只为一级下的子级复选框的数量而不是全部子级复选框的数量。他每次都要重新进来获取相应一级下的复选框数量。  各位帮忙看一下解决方法,非常感谢!
      

  5.   

    这个你不应该在这时候写啊
     这是DataBind事件啊
     应该有一个 按钮来取值吧
     不可能绑定的时候就取值吧
    这个东西呢最好用JS 来写 然后把值赋到HiddenField中 后台通过这个来 得到
    或者 你在事件中找到此Repeater 然后再通过E.XXX来得到CheckBox.然后For 再进行判断..
      

  6.   

    你用的方法只能取到选中状态有变化的选择列表,而且对后面的取值会覆盖掉前面的。合理的做法是删掉这个chkSelectJudge处理事件,在页面上加一个Button,在Button的OnClick事件进行取值。前台加上:<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    后台加上:protected void Button1_Click(object sender, EventArgs e)
    {
    ViewState["position"] = null; for (int index = 0; index < repposition.Items.Count; index++)
    {
    CheckBoxList chkl = (CheckBoxList)repposition.Items[index].FindControl("chkPostion"); string postion = "";
    for (int i = 0; i < chkl.Items.Count; i++)
    {
    if (chkl.Items[i].Selected)
    {
    postion += chkl.Items[i].Text + ",";
    }
    }
    ViewState["position"] = string.Concat(ViewState["position"], postion);
    }
    }
      

  7.   

    哎!搞了半天原来我第二种实现方式行的通,原因出在position变量,应该把它定义为全局变量。各位可以试试我的方法哦!呵呵...
      

  8.   

    如果回调时,某一级选择列表的选择状态都没有改变,那position中就不会包含这一级的选中的ID