のレンダリングSystem.Web.Contols.CheckBoxList
方法をカスタマイズするために派生したカスタム サーバー コントロールを作成しました。また、別のバインド可能なフィールドを追加して、メソッドCheckBoxList
内でフィールドの値を取得したいと考えていました。CheckBoxList.RenderItem()
作成するフィールドには、aCheckBoxListItem
がチェックされているかどうかを指定する値が含まれている必要があります。カスタム 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" />