Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4100852
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:37:15+00:00 2026-05-20T20:37:15+00:00

I have created a custom server control, deriving from System.Web.Contols.CheckBoxList to customize how a

  • 0

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" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:37:15+00:00Added an answer on May 20, 2026 at 8:37 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully an easy one, I have created a Custom Repeater control that extends System.Web.UI.WebControls.Repeater.
I have a my own custom web server control. I created separate CSS file
I have created a custom server control with properties implementing the ITemplate interface. It
I have created a custom Server control that processes some data, stores that data
I have created a simple Asp.Net custom control which automatically combines all the correct
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
Basically, I have created a custom control that uses an UpdatePanel, and as I
I have created custom MembershipUser, MembershipProvider and RolePrivoder classes. These all work and I
I have created a custom dialog for Visual Studio Setup Project using the steps
If have created a custom role within SqlServer which I added to the db__denydatareader

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.