自定义CheckBoxList

本文介绍了一种在ASP.NET中通过自定义控件的方式展示HTML input控件值的方法。通过覆盖Render方法并利用StringBuilder进行字符串操作,实现了在多列或多行布局下对input控件值的正确显示。

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

 在Html中可显示Value值代码如下:
protected override void Render(HtmlTextWriter writer)
 {

            StringBuilder sb = new StringBuilder();
            System.IO.TextWriter tw = new System.IO.StringWriter(sb);
            HtmlTextWriter OriginalStream = new HtmlTextWriter(tw);
            base.Render(OriginalStream);
            string s = sb.ToString();
            int start = 0;
            int end = s.Length;
            if (this.RepeatColumns == 0 || this.RepeatColumns == 1)
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    end = s.Length;
                    start = s.IndexOf("<input", start, end - start);
                    StringBuilder sbItemAttribute = new StringBuilder();
                    this.Items[i].Attributes.Render(new HtmlTextWriter(new System.IO.StringWriter(sbItemAttribute)));
                    s = s.Insert(start + 7, sbItemAttribute.ToString() + " ");
                    if (!String.IsNullOrEmpty(this.Items[i].Value))
                    {
                        s = s.Insert(start + 7, "value=/"" + this.Items[i].Value + "/"");
                    }
                    start = s.IndexOf("/>", start, s.Length - start);
                }
            }
            else//多列的处理
            {
                int arrcount = this.Items.Count;
                int icol = this.RepeatColumns;
                int icount = 1;
                int irow = 0;
                if (arrcount % icol > 0)
                {
                    irow = (arrcount / icol) + 1;
                }
                else
                {
                    irow = arrcount / icol;
                }

                for (int i = 0; i < irow; i++)
                {
                    for (int j = i; j < arrcount; j = j + irow)
                    {
                        if (j >= arrcount || icount > icol)
                        {
                            break;
                        }
                        end = s.Length;
                        start = s.IndexOf("<input", start, end - start);
                        StringBuilder sbItemAttribute = new StringBuilder();
                        this.Items[j].Attributes.Render(new HtmlTextWriter(new System.IO.StringWriter(sbItemAttribute)));
                        s = s.Insert(start + 7, sbItemAttribute.ToString() + " ");
                        if (!String.IsNullOrEmpty(this.Items[j].Value))
                        {
                            s = s.Insert(start + 7, "value=/"" + this.Items[j].Value + "/"");
                        }
                        start = s.IndexOf("/>", start, s.Length - start);
                        icount++;

                    }
                    icount = 1;
                }

            }
            writer.Write(s);
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值