CheckBoxList可显示value并可自由添加CheckBox的属性
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;
namespace MyWeb.Web.UI.WebControls
{
/// <summary>
/// Summary description for NewCheckBoxList.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:NewCheckBoxList runat=server></{0}:NewCheckBoxList>")]
public class NewCheckBoxList : System.Web.UI.WebControls.CheckBoxList
{
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();
//此替换替换了控件Id,所以PostBack后服务器无法取得这个CheckBoxList的值,如果需要服务器端处理,需要注释掉下边这句
s = s.Replace("<table id=\"" + this.ID.ToString() + "\"","<table id=\"" + this.ID.ToString() + "_t" + "\"");
int start = 0;
int end = s.Length;
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 + "\"");
}
//此替换替换了控件Id,所以PostBack后服务器无法取得这个CheckBoxList的值,如果需要服务器端处理,需要注释掉下边这句
s = s.Replace("\"" + this.ID.ToString() + "$" + i.ToString() + "\"", "\"" + this.ID.ToString() + "\"");
start = s.IndexOf("/>", start, s.Length - start);
}
writer.Write(s);
}
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;
namespace MyWeb.Web.UI.WebControls
{
/// <summary>
/// Summary description for NewCheckBoxList.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:NewCheckBoxList runat=server></{0}:NewCheckBoxList>")]
public class NewCheckBoxList : System.Web.UI.WebControls.CheckBoxList
{
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();
//此替换替换了控件Id,所以PostBack后服务器无法取得这个CheckBoxList的值,如果需要服务器端处理,需要注释掉下边这句
s = s.Replace("<table id=\"" + this.ID.ToString() + "\"","<table id=\"" + this.ID.ToString() + "_t" + "\"");
int start = 0;
int end = s.Length;
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 + "\"");
}
//此替换替换了控件Id,所以PostBack后服务器无法取得这个CheckBoxList的值,如果需要服务器端处理,需要注释掉下边这句
s = s.Replace("\"" + this.ID.ToString() + "$" + i.ToString() + "\"", "\"" + this.ID.ToString() + "\"");
start = s.IndexOf("/>", start, s.Length - start);
}
writer.Write(s);
}
}
}
此控件的客户端呈现和我们通常传统的Checkbox相同,name为控件的ID,value为Items的Vlaue,label为Items的Key,可以添加自己的属性,通过Items[i].Attributes.Add()来为每一个CheckBox增加属性。需要注意的是,这种修改后的控件只适合客户端操作数据,Postack后服务器端无法通过往常的Asp.net的方法获取这个CheckBoxList控件的值。
如果既想要客户端的CheckBox有Value属性,又想用正常的PostBack的方法操作CheckBoxList的数据,只要将其中替换控件ID的部分去掉,代码中有注释。