我创建了一个自定义服务器控件,派生自System.Web.Contols.CheckBoxList
自定义 aCheckBoxList
的呈现方式。我还想添加另一个可绑定字段并在CheckBoxList.RenderItem()
方法中获取该字段的值。我要创建的字段应包含一个指定是否CheckBoxListItem
选中 a 的值。我已经阅读了一些关于自定义 DataFields 的文章,但从未详细解释过。
我已经包括了我课堂的一部分,以更好地解释我似乎无法理解的内容。
public class ListedCheckBoxList : CheckBoxList
{
protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
if (itemType != ListItemType.Item)
return;
var item = base.Items[repeatIndex];
string cbxHtml = string.Format("<input type=\"checkbox\" value=\"{0}\" name=\"{1}\" /> {2}",
item.Value,
string.Concat(this.ClientID, repeatIndex),
item.IsChecked, // <-- My custom bindable field
item.Text);
writer.Write(cbxHtml);
}
}
在 .aspx 页面中使用此控件时,我试图像这样绑定它
<abc:ListedCheckBoxList ID="cbxList" runat="server"
DataValueField="UserId"
DataTextField="UserFullName"
DataIsCheckedField="UserIsActive" />