Extended CheckBoxList for selected binding

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.