首页 > 开发 > 综合 > 正文

一个带checkbox的webcontrol

2024-07-21 02:16:49
字体:
来源:转载
供稿:网友

谢谢大家,无以回报,贴点儿东西吧。

刚才看到下边不少人问给datatable或table添加一个选择框,实际上如果你可以自己做一个webcontrol来实现这个功能,下边是我做的一个带checkbox的webcontrol,由于注释比较全,所以我就不加说明了。

using system;
using system.collections ;
using system.web.ui.webcontrols ;
using system.web.ui ;



namespace bigeagle.web.webcontrols
{
    /// <summary>
    /// <br>自定义控件,带选择框的table</br>
    /// <br>author:  [email protected]</br>
    /// <br>date:    2001/10/10</br>
    /// <br>history: 2001/10/10 finished</br>
    /// </summary>
    /// <remarks>
    /// 继承system.web.ui.webcontrols.table,增加checkedvalue属性,
    /// 用于多项选择的场合,返回被选择的checktablerow的值的数组
    /// </remarks>
    public class checktable : system.web.ui.webcontrols.table
    {

        /// <summary>
        /// 是否使用checkbox
        /// </summary>
        /// <remarks>如果为false则不显示checkbox</remarks>
        private bool m_busecheck ;


        /// <summary>
        /// ֵ选择数组
        /// </summary>
        /// <remarks>返回选定的checktablerow的值</remarks>
        private arraylist m_arrcheckedvalue ;

        /// <summary>
        /// 取得选择数组的属性
        /// </summary>
        /// <remarks>只读属性</remarks>
        public arraylist checkedvalue
        {
            get
            {
                return this.m_arrcheckedvalue ;
            }
        }

        /// <summary>
        /// 存取是否使用checkbox的属性
        /// </summary>
        public bool usecheck
        {
            get
            {
                return this.m_busecheck ;
            }
            set
            {
                this.m_busecheck = value ;
            }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public checktable()
        {
            this.m_arrcheckedvalue = new arraylist() ;
            this.m_busecheck = true ;
        }

        /// <summary>
        /// 重载父类方法
        /// </summary>
        /// <param name="e"></param>
        protected override void onload(system.eventargs e)
        {
            if(this.m_busecheck)
            {
                try
                {
                    for(int i = 0 ; i < this.rows.count ; i++)
                    {
                        checktablerow ctr = (checktablerow)this.rows[i] ;
                        checkbox cb = (checkbox)ctr.findcontrol(ctr.value) ;
                        if(cb.checked)
                        {
                            this.m_arrcheckedvalue.add(ctr.value) ;
                        }
                    }

                }
                catch(exception)    //一般出现异常是因为有重复的控件id
                {
                    throw(new exception("出现重复的控件id")) ;
                }
            }
        }//end method
        

    }//end class
}//end method

using system;
using system.web.ui.webcontrols ;
using system.web.ui ;

namespace bigeagle.web.webcontrols
{
    /// <summary>
    /// <br>自定义控件,带选择框的tablerow</br>
    /// <br>author:  [email protected]</br>
    /// <br>date:    2001/10/10</br>
    /// <br>history: 2001/10/10 finished</br>
    /// </summary>
    /// <remarks>
    /// 继承system.web.ui.webcontrols.tablerow , 作为自定义控件checktable的tablerow,
    /// 与tablerow相比,增加一个值属性和一个checkbox
    /// </remarks>
    public class checktablerow : system.web.ui.webcontrols.tablerow
    {
        /// <summary>
        /// 值
        /// </summary>
        private string m_strvalue ;

        /// <summary>
        /// 是否使用checkbox
        /// </summary>
        /// <remarks>如果为false则不显示checkbox</remarks>
        private bool m_busecheck ;

        /// <summary>
        /// 存取value的属性
        /// </summary>
        public string value
        {
            get
            {
                return this.m_strvalue ;
            }
            set
            {
                this.m_strvalue = value ;
            }
        }

        /// <summary>
        /// 存取是否使用checkbox的属性
        /// </summary>
        public bool usecheck
        {
            get
            {
                return this.m_busecheck ;
            }
            set
            {
                this.m_busecheck = value ;
            }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public checktablerow()
        {
            this.m_strvalue = "" ;
            this.m_busecheck = true ;

            
        }

        /// <summary>
        /// 重载父类方法
        /// </summary>
        /// <returns>控件结合</returns>
        /// <remarks>增加一个带有checkbox的td</remarks>
        protected override system.web.ui.controlcollection createcontrolcollection()
        {
            controlcollection cc = base.createcontrolcollection() ;
            if(this.m_busecheck)
            {
                tablecell td = new tablecell() ;
                td.backcolor = this.backcolor ;
                td.verticalalign = verticalalign.top ;
                checkbox cb = new checkbox() ;
                cb.id = this.m_strvalue ;
                td.controls.add(cb) ;
                cc.add(td) ;
            }
            return cc ;
        }//end method


    }//end class
}//end namespace
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表