我创建了一个自定义服务器控件,源自System.Web.Contols.CheckBoxList
定制如何CheckBoxList
被渲染。我还想添加另一个可绑定字段并获取该字段的值CheckBoxList.RenderItem()
方法。我想要创建的字段应该包含一个值,指定是否CheckBoxListItem
已检查。我读过一些有关自定义数据字段的文章,但从未详细解释过。
我在课堂上添加了一部分内容,以更好地解释我似乎无法理解的内容。
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" />
这是我大约一年前写的一个版本。我希望能够绑定选中的状态以及各个项目的工具提示。希望能帮助到你...
public class CheckBoxList_Extended : CheckBoxList
{
/// <summary>
/// Gets or sets the name of the data property to bind to the tooltip attribute of the individual CheckBox.
/// </summary>
[DefaultValue("")]
public string DataTooltipField
{
get
{
string value = base.ViewState["DataTooltipField"] as string;
if (value == null)
value = "";
return value;
}
set
{
if (value == null || value.Trim() == "")
{
base.ViewState.Remove("DataTooltipField");
}
else
{
base.ViewState["DataTooltipField"] = value.Trim();
}
}
}
/// <summary>
/// Gets or sets the name of the data property to bind to the Checked property of the individual CheckBox.
/// </summary>
[DefaultValue("")]
public string DataCheckedField
{
get
{
string value = base.ViewState["DataCheckedField"] as string;
if (value == null)
value = "";
return value;
}
set
{
if (value == null || value.Trim() == "")
{
base.ViewState.Remove("DataCheckedField");
}
else
{
base.ViewState["DataCheckedField"] = value.Trim();
}
}
}
protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
{
if (dataSource != null)
{
string dataSelectedField = this.DataCheckedField;
string dataTextField = this.DataTextField;
string dataTooltipField = this.DataTooltipField;
string dataValueField = this.DataValueField;
string dataTextFormatString = this.DataTextFormatString;
bool dataBindingFieldsSupplied = (dataTextField.Length != 0) || (dataValueField.Length != 0);
bool hasTextFormatString = dataTextFormatString.Length != 0;
bool hasTooltipField = dataTooltipField.Length != 0;
bool hasSelectedField = dataSelectedField.Length != 0;
if (!this.AppendDataBoundItems)
this.Items.Clear();
if (dataSource is ICollection)
this.Items.Capacity = (dataSource as ICollection).Count + this.Items.Count;
foreach (object dataItem in dataSource)
{
ListItem item = new ListItem();
if (dataBindingFieldsSupplied)
{
if (dataTextField.Length > 0)
{
item.Text = DataBinder.GetPropertyValue(dataItem, dataTextField, null);
}
if (dataValueField.Length > 0)
{
item.Value = DataBinder.GetPropertyValue(dataItem, dataValueField, null);
}
}
else
{
if (hasTextFormatString)
{
item.Text = string.Format(CultureInfo.CurrentCulture, dataTextFormatString, new object[] { dataItem });
}
else
{
item.Text = dataItem.ToString();
}
item.Value = dataItem.ToString();
}
if (hasSelectedField)
{
item.Selected = (bool)DataBinder.GetPropertyValue(dataItem, dataSelectedField);
}
if (hasTooltipField)
{
string tooltip = DataBinder.GetPropertyValue(dataItem, dataTooltipField, null);
if (tooltip != null && tooltip.Trim() != "")
{
item.Attributes["title"] = tooltip;
}
}
this.Items.Add(item);
}
}
base.PerformDataBinding(null);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)