Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > CheckBoxList with Multiple selection set Declaratively

Reply
Thread Tools

CheckBoxList with Multiple selection set Declaratively

 
 
Simon
Guest
Posts: n/a
 
      09-12-2007
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

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      09-13-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
Reply With Quote
 
 
 
 
Simon
Guest
Posts: n/a
 
      09-13-2007
Thanks a lot for the example Teemu. I'll try that right away.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
set selected value declaratively to dropdownlist podi ASP .Net 3 10-03-2006 03:57 PM
'property cannot be set declaratively.'?? cannontrodder ASP .Net 0 09-06-2006 10:03 AM
datasource cannot be set declaratively =?Utf-8?B?RGFiYmxlcg==?= ASP .Net 2 04-03-2006 11:40 AM
How to declaratively set page property in aspx file? =?Utf-8?B?d2FsdGVy?= ASP .Net 2 03-29-2006 05:06 PM
Set Custom Page properties declaratively =?Utf-8?B?UmljaGFyZCBCcm93bg==?= ASP .Net 2 12-02-2005 04:47 PM



Advertisments