I have created a custom server control, deriving from System.Web.Contols.CheckBoxList
to customize how a CheckBoxList
is rendered. I also wanted to add another bindable field and get the value of the field within the CheckBoxList.RenderItem()
method. The field I want to create, should contain a value specifying whether a CheckBoxListItem
is checked. I've read some articles regarding custom DataFields, but it never gets explained in detail.
I've included a portion of my class to better explain what I can't seem to understand.
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);
}
}
When using this control in the .aspx page, I'm attempting to bind it like this
<abc:ListedCheckBoxList ID="cbxList" runat="server"
DataValueField="UserId"
DataTextField="UserFullName"
DataIsCheckedField="UserIsActive" />