一、自定义控件之CheckBoxList

因需求不同
最近公司项目要求开发自定义控件,看了些前辈的代码……现在结合自己的项目。
自定义CheckBoxList 主要功能: 全选功能、获取任一checkbox的Text或者Value值
后台CS:
一、自定义控件之CheckBoxList一、自定义控件之CheckBoxListView Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI; using System.ComponentModel; namespace FumaCRM_BS.WebControls { #region CheckBoxList /// /// 多项选择复选框组 /// [DefaultProperty("Text")] [ToolboxData("<{0}:FBSCheckBoxList runat=server>")] public class FBSCheckBoxList : System.Web.UI.WebControls.CheckBoxList { private const char SEPARATOR = '|';//分隔符 #region Field private bool selectAll;//全选 #endregion [Category("Appearance"), Browsable(true), Description("是否全选"), DefaultValue(false)] public bool SelectAll { set { selectAll = value; } get { return selectAll; } } /// /// 选中项的Text值 /// [Category("Appearance"), Browsable(true), Description(" 选中项的Text值"), DefaultValue("")] public string NewSelectedText { set { //newSelectText = value; if (value != null && value != string.Empty && value != "") { this.SelectedIndex = -1; //重新选择 string[] textList = value.Split(new char[] { SEPARATOR }); for (int i = 0; i < textList.Length; i++) { for (int j = 0; j < this.Items.Count; j++) { if (this.Items[j].Text == textList[i]) { this.Items[j].Selected = true; } } } } } get { //return newSelectText; string textList = string.Empty; for (int i = 0; i < this.Items.Count; i++) { if (this.Items[i].Selected == true) { textList += this.Items[i].Text + SEPARATOR.ToString(); } } if (textList != string.Empty) { return textList.Substring(0, textList.Length - 1); } else { return textList; } } } /// /// 属性:选中项的Value值 /// [Category("Appearance"), Browsable(true), Description(" 选中项的Value值"), DefaultValue("")] public string NewSelectedValue { set { if (value != null && value != string.Empty && value != "") { this.SelectedIndex = -1; //重新选择 string[] valueList = value.Split(new char[] { SEPARATOR }); for (int i = 0; i < valueList.Length; i++) { for (int j = 0; j < this.Items.Count; j++) { if (this.Items[j].Value == valueList[i]) { this.Items[j].Selected = true; } } } } } get { string valueList = string.Empty; for (int i = 0; i < this.Items.Count; i++) { if (this.Items[i].Selected == true) { valueList += this.Items[i].Value + SEPARATOR.ToString(); } } if (valueList != string.Empty) { return valueList.Substring(0, valueList.Length - 1); } else { return valueList; } } } protected override void _disibledevent=>base.OnPreRender(e); if (selectAll) { for (int i = 0; i < this.Items.Count; i++) { Items[i].Selected = true; } } else { for (int i = 0; i < this.Items.Count; i++) { if (Items[i].Selected) Items[i].Selected = true; else Items[i].Selected = false; } } } protected override void Render(HtmlTextWriter writer) { base.Render(writer); } } #endregion }
前台aspx 同行在工具中 右键 选择项 引入刚才编译好的dll
直接拖拽到aspx中即可 代码如下
View Code
选项A 选项B 选项C 选项D 选项E 选项F 选项G 选项H ="Button11_Click" />
="Button2_Click" />
="Button3_Click" />
="Button4_Click" />
="Button5_Click" />

页面如下


可以跟不同的需求 实现全选,获取任意checkbox值及value 也可以通过设置默认text和value
之前做的项目一直是用到服务器端控件和html控件,自己自定义开发控件 刚接触 所以 有什么不妥的 请大家指教 谢谢
如果 您有这块自定义代码 可否 email给我一份 感谢 [email protected]
Tags: 

延伸阅读

最新评论

发表评论