CheckBoxList with Multiple selection set Declaratively

S

Simon

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
 
T

Teemu Keiski

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top