CheckBoxList with Multiple selection set Declaratively

Discussion in 'ASP .Net Building Controls' started by Simon, Sep 12, 2007.

  1. Simon

    Simon Guest

    Hi,

    I'd like to extend the CheckBoxList control so that I can set multiple
    selection declaratively. Right now, I'm thinking of simply giving it a
    string containing all the selected values, each values separated by a
    comma. So, baiscally I have this :

    public class TSGControlCheckBoxList :
    System.Web.UI.WebControls.CheckBoxList
    {
    public string SelectedItems
    {
    set { }
    get { }
    }
    }

    Problem is, I'm a complete newbie when it comes to making my own Web
    Controls and I have a few questions:

    - In the set method for SelectedItems, where should I store my string?
    In the viewstate?

    - During which phase of the lifecycle should I set the ListItem as
    checked by the values in SelectedItems?

    - What should the get look like? Would it work to simply go trough all
    the ListItem and build my string like that or would an error occur
    because they might not yet be built correctly?

    So, if you can offer any insight or an example, your help would be
    appreciated.

    Simon Picard
     
    Simon, Sep 12, 2007
    #1
    1. Advertising

  2. Simon

    Teemu Keiski Guest

    Hi,

    if you think design of CheckBoxList's SelectedItem etc member, you'll note
    it's thought to be used basically almost "at any time".
    Basically point is that when accessing SelectedItem, it is assumed you have
    items and otherwise an exception is thrown. I think it's quite safe to use
    this approach with this property. Only exception indeed is that you needf to
    delay setting the selections, it could be done in CreateChildControls or
    OnInit.

    here's an example:

    namespace Samples
    {
    public class MyCheckBoxList : CheckBoxList
    {
    private string _setItems = null;
    public string SelectedItems
    {
    get
    {
    //Might be overkill to instantiate Sb every time if you
    don't have many items

    System.Text.StringBuilder sb = new
    System.Text.StringBuilder();
    foreach (ListItem item in this.Items)
    {
    if (item.Selected)
    {
    sb.Append(item.Value);
    sb.Append(",");
    }
    }

    return sb.ToString().TrimEnd(',');
    }
    set
    {
    if (value == null) throw new ArgumentNullException("value");
    _setItems = value;

    }
    }

    protected override void CreateChildControls()
    {
    base.CreateChildControls();
    if (_setItems != null)
    {
    foreach (string stringVal in _setItems.Split(','))
    {
    ListItem item = this.Items.FindByValue(stringVal);
    if (item != null)
    {
    item.Selected = true;
    }
    }
    _setItems = null;
    }
    }

    }
    }

    and usage:

    <%@ Register Namespace="Samples" TagPrefix="cc" %>
    ....
    <cc:MyCheckBoxList ID="CheckBoxList1" runat="server" SelectedItems="2,4">
    <Items>
    <asp:ListItem Text="Text 1" Value="1" />
    <asp:ListItem Text="Text 2" Value="2" />
    <asp:ListItem Text="Text 3" Value="3" />
    <asp:ListItem Text="Text 4" Value="4" />
    <asp:ListItem Text="Text 5" Value="5" />
    </Items>
    </cc:MyCheckBoxList>


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net



    "Simon" <> wrote in message
    news:...
    > Hi,
    >
    > I'd like to extend the CheckBoxList control so that I can set multiple
    > selection declaratively. Right now, I'm thinking of simply giving it a
    > string containing all the selected values, each values separated by a
    > comma. So, baiscally I have this :
    >
    > public class TSGControlCheckBoxList :
    > System.Web.UI.WebControls.CheckBoxList
    > {
    > public string SelectedItems
    > {
    > set { }
    > get { }
    > }
    > }
    >
    > Problem is, I'm a complete newbie when it comes to making my own Web
    > Controls and I have a few questions:
    >
    > - In the set method for SelectedItems, where should I store my string?
    > In the viewstate?
    >
    > - During which phase of the lifecycle should I set the ListItem as
    > checked by the values in SelectedItems?
    >
    > - What should the get look like? Would it work to simply go trough all
    > the ListItem and build my string like that or would an error occur
    > because they might not yet be built correctly?
    >
    > So, if you can offer any insight or an example, your help would be
    > appreciated.
    >
    > Simon Picard
    >
     
    Teemu Keiski, Sep 13, 2007
    #2
    1. Advertising

  3. Simon

    Simon Guest

    Thanks a lot for the example Teemu. I'll try that right away.
     
    Simon, Sep 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmljaGFyZCBCcm93bg==?=

    Set Custom Page properties declaratively

    =?Utf-8?B?UmljaGFyZCBCcm93bg==?=, Dec 2, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    418
    =?Utf-8?B?UmljaGFyZCBCcm93bg==?=
    Dec 2, 2005
  2. =?Utf-8?B?d2FsdGVy?=

    How to declaratively set page property in aspx file?

    =?Utf-8?B?d2FsdGVy?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    556
    =?Utf-8?B?d2FsdGVy?=
    Mar 29, 2006
  3. =?Utf-8?B?RGFiYmxlcg==?=

    datasource cannot be set declaratively

    =?Utf-8?B?RGFiYmxlcg==?=, Apr 3, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    3,157
    =?Utf-8?B?RGFiYmxlcg==?=
    Apr 3, 2006
  4. cannontrodder

    'property cannot be set declaratively.'??

    cannontrodder, Sep 6, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    941
    cannontrodder
    Sep 6, 2006
  5. podi
    Replies:
    3
    Views:
    27,055
Loading...

Share This Page