我創建了一個自定義服務器控件,派生自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