一个额外的可绑定场ASP.NET服务器控件 [英] ASP.NET Server control with an additional bindable field
问题描述
我创建了一个自定义服务器控件,从 System.Web.Contols.CheckBoxList
导出自定义如何的CheckBoxList
被渲染。我也想在 CheckBoxList.RenderItem()
方法中添加其他绑定字段,获取字段的值。我想创建领域,应该包含一个值,指定是否 CheckBoxListItem
被选中。我读过关于自定义数据域的一些文章,但它从来没有被详细解释。
我包括我的课的一部分,以更好地解释似乎是我无法理解的。
公共类ListedCheckBoxList:的CheckBoxList
{
保护覆盖无效RenderItem(ListItemType ITEMTYPE,INT repeatIndex,RepeatInfo repeatInfo,HtmlTextWriter的作家)
{
如果(ITEMTYPE!= ListItemType.Item)
返回; VAR项目= base.Items [repeatIndex] 字符串cbxHtml =的String.Format(<输入类型= \\复选框\\价值= \\{0} \\NAME = \\{1} \\/> {2},
item.Value,
string.Concat(this.ClientID,repeatIndex)
item.IsChecked,//< - 我的自定义字段绑定
item.Text); writer.Write(cbxHtml);
}
}
当使用在.aspx页面这种控制,我试图绑定像这样
< ABC:ListedCheckBoxList ID =cbxList=服务器
DataValueField =用户ID
DataTextField =UserFullName
DataIsCheckedField =UserIsActive/>
下面是一个版本,我写了一年多前。我希望能够绑定的检查状况以及对单个项目的工具提示。希望它可以帮助...
公共类CheckBoxList_Extended:的CheckBoxList
{
///<总结>
///获取或设置数据属性的名称绑定到单独的CheckBox的提示属性。
///< /总结>
[默认值()]
公共字符串DataTooltipField
{
得到
{
字符串值= base.ViewState [DataTooltipField]作为字符串;
如果(价值== NULL)
值=;
返回值;
}
组
{
如果(价值== NULL || value.Trim()==)
{
base.ViewState.Remove(DataTooltipField);
}
其他
{
base.ViewState [DataTooltipField] = value.Trim();
}
}
}
///<总结>
///获取或设置数据属性的名称绑定到单独的CheckBox的Checked属性。
///< /总结>
[默认值()]
公共字符串DataCheckedField
{
得到
{
字符串值= base.ViewState [DataCheckedField]作为字符串;
如果(价值== NULL)
值=;
返回值;
}
组
{
如果(价值== NULL || value.Trim()==)
{
base.ViewState.Remove(DataCheckedField);
}
其他
{
base.ViewState [DataCheckedField] = value.Trim();
}
}
} 保护覆盖无效PerformDataBinding(System.Collections.IEnumerable数据源)
{
如果(数据源!= NULL)
{
字符串dataSelectedField = this.DataCheckedField;
字符串dataTextField = this.DataTextField;
字符串dataTooltipField = this.DataTooltipField;
字符串dataValueField = this.DataValueField;
字符串dataTextFormatString = this.DataTextFormatString; 布尔dataBindingFieldsSupplied =(dataTextField.Length!= 0)|| (dataValueField.Length!= 0);
布尔hasTextFormatString = dataTextFormatString.Length!= 0;
布尔hasTooltipField = dataTooltipField.Length!= 0;
布尔hasSelectedField = dataSelectedField.Length!= 0; 如果(!this.AppendDataBoundItems)
this.Items.Clear(); 如果(数据源是的ICollection)
this.Items.Capacity =(数据源为ICollection的).Count之间+ this.Items.Count; 的foreach(在数据源对象的DataItem)
{
列表项项=新的ListItem(); 如果(dataBindingFieldsSupplied)
{
如果(dataTextField.Length大于0)
{
item.Text = DataBinder.GetPropertyValue(DataItem的,dataTextField,NULL);
}
如果(dataValueField.Length大于0)
{
item.Value = DataBinder.GetPropertyValue(DataItem的,dataValueField,NULL);
}
}
其他
{
如果(hasTextFormatString)
{
item.Text =的String.Format(CultureInfo.CurrentCulture,dataTextFormatString,新的对象[] {DataItem的});
}
其他
{
item.Text = dataItem.ToString();
}
item.Value = dataItem.ToString();
}
如果(hasSelectedField)
{
item.Selected =(布尔)DataBinder.GetPropertyValue(DataItem的,dataSelectedField);
}
如果(hasTooltipField)
{
字符串提示= DataBinder.GetPropertyValue(DataItem的,dataTooltipField,NULL);
如果(提示=空&安培;!&安培;!tooltip.Trim()=)
{
item.Attributes [标题] =提示;
}
}
this.Items.Add(项目);
}
}
base.PerformDataBinding(NULL);
}
}
I have created a custom server control, deriving from System.Web.Contols.CheckBoxList
to customize how a CheckBoxList
is rendered. I also wanted to add another bindable field and get the value of the field within the CheckBoxList.RenderItem()
method. The field I want to create, should contain a value specifying whether a CheckBoxListItem
is checked. I've read some articles regarding custom DataFields, but it never gets explained in detail.
I've included a portion of my class to better explain what I can't seem to understand.
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);
}
}
When using this control in the .aspx page, I'm attempting to bind it like this
<abc:ListedCheckBoxList ID="cbxList" runat="server"
DataValueField="UserId"
DataTextField="UserFullName"
DataIsCheckedField="UserIsActive" />
Here is a version I wrote a year or so ago. I wanted to be able to bind the checked status as well as a tooltip for the individual items. Hope it helps...
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);
}
}
这篇关于一个额外的可绑定场ASP.NET服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!