我创建了一个自定义服务器控件,派生自System.Web.Contols.CheckBoxList
以定制CheckBoxList
的呈现方式。我还想添加另一个可绑定字段,并获取CheckBoxList.RenderItem()
方法中字段的值。我想要创建的字段应该包含一个值,指定是否检查CheckBoxListItem
。我已经阅读了一些关于自定义DataFields的文章,但它从来没有得到详细解释。带有附加绑定字段的ASP.NET服务器控件
我已经包括了我的课的一部分,以更好地解释我似乎无法理解的内容。
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" />
您的问题回答了我的问题,谢谢! – guanome 2013-01-07 19:27:47
复选框的选中状态是否已张贴?我必须设置唯一的ID才能使其工作。 – guanome 2013-01-08 16:17:13