一个带checkbox的webcontrol
点击次数:24 次 发布日期:2008-11-27 01:38:39 作者:源代码网
|
刚才看到下边不少人问给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: Bigeagle@163.net</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> /// ɦ멦BR> /// </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: Bigeagle@163.net</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< 源代码网供稿. |
