Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Custom CheckBoxList with SelectedValues Property

0.00/5 (No votes)
19 Nov 2010 1  
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
C#
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<string></string> 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.
C#
[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" />

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here