Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET
Tip/Trick

Custom CheckBoxList with SelectedValues Property

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
19 Nov 2010CPOL 14.8K   3   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) Fincons Group
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice one Pin
Member 1338498229-Aug-17 21:07
Member 1338498229-Aug-17 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.