积极答复者
自定义继承自CheckBoxList的控件报错?

问题
-
我根据网上的文章自定义了一个继承CheckBoxList的自定义控件,使其能支持多选,功能是实现了,但会报“元素“ListItem”不是已知元素。原因可能是网站中存在编译错误”的错误,请教高手应该怎么修改?代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Dragon { [DefaultProperty("Text")] [ToolboxData("<{0}:CheckBoxList runat=server></{0}:CheckBoxList>")] public class CheckBoxList : System.Web.UI.WebControls.CheckBoxList { //[Bindable(true)] //[Category("Appearance")] //[DefaultValue("")] //[Localizable(true)] private const char SEPARATOR = ',';//分隔符 /// <summary> /// 选中项的Text值 /// </summary> [Browsable(true)] [DefaultValue("")] public string NewSelectedText { set { if (value != null && value != string.Empty && value != "") { //取消原先选择项 //for (int i = 0; i < this.Items.Count; i++) //{ // this.Items[i].Selected = false; //} 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 { 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; } } } /// <summary> /// 属性:选中项的Value值 /// </summary> [Browsable(true)] [DefaultValue("")] public string NewSelectedValue { set { if (value != null && value != string.Empty && value != "") { //取消原先选择项 //for (int i = 0; i < this.Items.Count; i++) //{ // this.Items[i].Selected = false; //} 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 Render(System.Web.UI.HtmlTextWriter writer) { base.Render(writer); } } }
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="Dragon" Namespace="Dragon" TagPrefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <cc1:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem Value="1"> </asp:ListItem> <asp:ListItem Value="2"> </asp:ListItem> <asp:ListItem Value="3"> </asp:ListItem> </cc1:CheckBoxList> </div> </form> </body> </html>
答案
全部回复
-
两处修改:
1)删除以下代码片段:
protected override void Render(System.Web.UI.HtmlTextWriter writer) { base.Render(writer); }
2)不要忘记添加Text:
<cc1:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem Text="aaa" Value="aaa" /> <asp:ListItem Text="bbb" Value="bb" /> </cc1:CheckBoxList>