In the CheckBoxList, DataTextField and DataValueField can be used to set the attributes of the bound object. However, it is a pity that there is no way to bind an Item to the CheckBoxList to check whether it is selected. So I want to extend the CheckBoxList so that the control can be bound to the Checked status. The specific method is as follows:
(1) create a Web server control project and add the CheckBoxListWithCheckBind class of the Web server control.
(2) inherit this class from CheckBoxList.
public class CheckBoxListWithCheckBind : CheckBoxList
(3) Add the attribute DataCheckedField to specify the attribute name string bound to the Checked status.
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string DataCheckedField
{
get
{
String s = (String)ViewState["DataCheckedField"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["DataCheckedField"] = value;
}
}
(4) Add the BindChecked method to bind the Selected Attribute of each Item in the CheckBoxList. Here we use the Items. FindByValue method to find the Item. Here we think the Value of each Item is different. If the Text of an Item is different, you can also use the Items. FindByText method.
private void BindChecked()
{
var dataSource = this.DataSource as IEnumerable;
if(dataSource==null)
{
return;
}
foreach (object obj2 in dataSource)
{
var value = DataBinder.GetPropertyValue(obj2, DataValueField, null);
ListItem item = this.Items.FindByValue(value);
if (DataCheckedField.Length > 0)
{
item.Selected = Convert.ToBoolean(DataBinder.GetPropertyValue(obj2, DataCheckedField, null));
}
}
}
(5) override the OnDataBinding method. Call the previously written BindChecked method after the OnDataBinding method of the base class.
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
BindChecked();
}
The specific code is as follows:
Complete code
Public class CheckBoxListWithCheckBind: CheckBoxList
{
[Bindable (true)]
[Category ("Appearance")]
[DefaultValue ("")]
[Localizable (true)]
Public string DataCheckedField
{
Get
{
String s = (String) ViewState ["DataCheckedField"];
Return (s = null )? String. Empty: s );
}
Set
{
ViewState ["DataCheckedField"] = value;
}
}
Protected override void OnDataBinding (EventArgs e)
{
Base. OnDataBinding (e );
BindChecked ();
}
Private void BindChecked ()
{
Var dataSource = this. DataSource as IEnumerable;
If (dataSource = null)
{
Return;
}
Foreach (object obj2 in dataSource)
{
Var value = DataBinder. GetPropertyValue (obj2, DataValueField, null );
ListItem item = this. Items. FindByValue (value );
If (DataCheckedField. Length> 0)
{
Item. Selected = Convert. ToBoolean (DataBinder. GetPropertyValue (obj2, DataCheckedField, null ));
}
}
}
}
The following method is simple. You can directly write the DataCheckedField attribute of the control on the aspx page:
<cc1:CheckBoxListWithCheckBind ID="cbxl" runat="server" DataTextField="CompanyName" DataValueField="CompanyCode" DataCheckedField="IsChecked">
</cc1:CheckBoxListWithCheckBind>