CheckBoxList动态绑定数据按需换行

该博客介绍了如何在CheckBoxList中动态绑定数据并按标签类别进行换行展示。通过在数据中插入空标签并控制其显示,实现了根据标签关系分行的效果。主要涉及方法包括填充空标签(FillEmptyTag)和隐藏空标签(HideEmptyTag)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /// <summary>
    /// 填充空标签
    /// (根据标签类别分组,根据每行的数量填充相应的空标签以达到根据标签关系分行的目的)
    /// </summary>
    /// <param name="tagList"></param>
    /// <param name="repeatColumns"></param>
    private void FillEmptyTag(List<tags> tagList, int repeatColumns)
    {
        var list = tagList.GroupBy(x => x.tagTypeId)
            .Select(y => (new { tagTypeId = y.Key, count = y.Count() })).ToList();

        int index = 0; // 插入空标签的索引
        foreach (var item in list)
        {
            index += item.count;

            // 算出空标签的数量
            int count = repeatColumns - item.count % repeatColumns;

            if (count > 0 && count != repeatColumns)
            {
                for (int i = 0; i < count; i++)
                {
                    // 初始化空标签
                    tags emptyTag = new tags();
                    emptyTag.tagId = "0";
                    emptyTag.name = string.Empty;

                    tagList.Insert(index + i, emptyTag);
                }

                // 插入空标签之后,更新index
                index += count;
            }
        }
    }

    /// <summary>
    /// 隐藏CheckBoxList控件中的空标签
    /// </summary>
    /// <param name="chkl"></param>
    private void HideEmptyTag(CheckBoxList chkl)
    {
        foreach (ListItem li in chkl.Items)
        {
            if ("0".Equals(li.Value))
            {
                li.Selected = false;
                li.Attributes.Add("style", "visibility:hidden");
            }
        }
    }
动态绑定“标签”,要按照标签类别换行,可以在绑定的数据中插入相应的空数据,然后隐藏空数据,就可以实现换行的效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值