【发布时间】:2011-07-21 13:08:14
【问题描述】:
我创建了一个自定义服务器控件,从 System.Web.Contols.CheckBoxList 派生来自定义 CheckBoxList 的呈现方式。我还想添加另一个可绑定字段并在CheckBoxList.RenderItem() 方法中获取该字段的值。我要创建的字段应包含一个值,该值指定是否选中 CheckBoxListItem。我读过一些关于自定义 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" />
【问题讨论】:
-
你的问题回答了我的问题,谢谢!
-
复选框的选中状态是否已发布?我必须设置唯一 ID 才能让它工作。
标签: c# asp.net asp.net-controls