65.9K
CodeProject is changing. Read more.
Home

Custom CheckBoxList with SelectedValues Property

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (3 votes)

Nov 19, 2010

CPOL
viewsIcon

15229

In this short article we wil face the problem of creating a custom control which extends CheckBoxList and add Selected Values Property

How many times did you faced the problem of using a CheckBoxList control in your web application and use it as control parameter of an ObjectDataSource control? I face similar situation and I searched over internet and I didn't found any good work around which is not involve javascript functions on selected event or using hidden fields used to store values of selected items. What If I do not want to use javascript? So I decided to create a custom control which extends CheckBoxList and add a simple property called SelectedValues. It will retrieve a list of selected values without using any javascript at all. So here this is
public class CustomCheckBoxList : System.Web.UI.WebControls.CheckBoxList
{
    public ListItems SelectedValues
    {
        get
        {
            ListItems selected = new ListItems();
            foreach (ListItem item in this.Items)
            {
                if (item.Selected)
                    selected.Add(item.Value);
            }
            return selected;
        }
    }
ListItems is a custom List.It extends List and implements IConvertible interface, because I need to use it in an ObjectDataSource as a control parameter and then it's necessary to convert it into a string. The conversion is done combining all selected values using comma as separator.
    [Serializable]
    public class ListItems : List<String>, IConvertible
    {
        #region IConvertible Members

        public TypeCode GetTypeCode()
        {
            return this.GetTypeCode();
        }
        /// <summary>
        /// Returns logic evaluation of selected values (in &)
        /// </summary>
        /// <param name="provider"></param>
        /// <returns>True if all values are true , false otherwise</returns>
        public bool ToBoolean(IFormatProvider provider)
        {
            foreach (string item in this)
            {
                if (!Convert.ToBoolean(item, provider))
                    return false;
            }
            return true;
        }

        public byte ToByte(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public char ToChar(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public DateTime ToDateTime(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public decimal ToDecimal(IFormatProvider provider)
        {
            decimal retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToDecimal(item, provider);
            }
            return retVal;
        }

        public double ToDouble(IFormatProvider provider)
        {
            double retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToDouble(item, provider);
            }
            return retVal;
        }

        public short ToInt16(IFormatProvider provider)
        {
            short retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToInt16(item, provider);
            }
            return retVal;
        }

        /// <summary>
        /// Returns the sum of selected values
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        public int ToInt32(IFormatProvider provider)
        {
            int retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToInt32(item, provider);
            }
            return retVal;
        }

        public long ToInt64(IFormatProvider provider)
        {
            long retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToInt64(item, provider);
            }
            return retVal;
        }

        public sbyte ToSByte(IFormatProvider provider)
        {
            sbyte retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToSByte(item, provider);
            }
            return retVal;
        }

        public float ToSingle(IFormatProvider provider)
        {
            float retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToInt64(item, provider);
            }
            return retVal;
        }

        /// <summary>
        /// Returns the String Concatenation of selected values
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        public string ToString(IFormatProvider provider)
        {
            if (this.Count == 0)
                return null;
            return string.Join(",", this.ToArray());
        }

        public object ToType(Type conversionType, IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        public ushort ToUInt16(IFormatProvider provider)
        {
            ushort retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToUInt16(item, provider);
            }
            return retVal;
        }

        public uint ToUInt32(IFormatProvider provider)
        {
            uint retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToUInt32(item, provider);
            }
            return retVal;
        }

        public ulong ToUInt64(IFormatProvider provider)
        {
            ulong retVal = 0;

            foreach (string item in this)
            {
                retVal += Convert.ToUInt64(item, provider);
            }
            return retVal;
        }

        #endregion
    }
Finally we can use our custom property with an ObjectDataSource control
<asp:ControlParameter ControlID="cblStates"Name="states" PropertyName="SelectedValues"Type="String" />