隐藏

C# CheckBoxList绑定值,设置及获取

发布:2020/12/29 8:41:04作者:管理员 来源:本站 浏览次数:986

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">
                            <ItemTemplate>
                                <p >
                                <asp:HiddenField ID="hfGetId" Value='<%#Eval("TMIdInt") %>' runat="server" />
                                <span style="color:red;"><%#Eval("NameStr") %></span>
                                <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal"></asp:CheckBoxList>
                                </p>
                            </ItemTemplate>

                        </asp:Repeater>


Maticsoft.Model.DR_TRoles _TRoles = _bll_TRoles.GetModel(int.Parse(user.TRIdInt.ToString()));
        if (_TRoles != null && _TRoles.Cids != null)
        {
            DataSet ds = _bll_TMenus.GetList(" TMIdInt in(" + _TRoles.Cids + ") order by SortTint ASC");
            Repeater1.DataSource = ds;
            Repeater1.DataBind();

            string[] arr = user.Cids.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
                foreach (Control c in this.Repeater1.Controls)
                {
                    CheckBoxList cbx = (CheckBoxList)c.FindControl("CheckBoxList1");
                    if (cbx != null)
                    {
                        foreach (ListItem item in cbx.Items)
                        {
                            if (item.Value==arr[i])
                            {
                                item.Selected = true;
                            }
                        }
                    }
                }
            }
        }



string cids = string.Empty;
            foreach (Control c in this.Repeater1.Controls)
            {
                CheckBoxList cbx = (CheckBoxList)c.FindControl("CheckBoxList1");
                if (cbx != null)
                {
                    foreach (ListItem item in cbx.Items)
                    {
                        if (item.Selected == true)
                        {
                            cids += item.Value+",";
                        }
                    }
                }
            }
            user.Cids = cids.TrimEnd(',');