2

How to get which value items where selected from a CheckBoxList using Request.Form?

I see these 2 form keys:

[12]: "ctl00$MainContent$cblTimeOfDay$0"
[13]: "ctl00$MainContent$cblTimeOfDay$3"

0 and 3 are the selected values from my check box list which has 4 items.

I'd need to find those values programmaticlaly on Page_Init

thanks,

The Light
  • 26,341
  • 62
  • 176
  • 258

2 Answers2

0

I'm not sure about accessing these via the Request.Form. Can't you access the strongly-typed CheckBoxList control itself? This article provides a simply method that accepts a CheckBoxList and returns all the selected values; you may update this to return a reference to the selected item, or any other specifics you require:

public string[] CheckboxListSelections(System.Web.UI.WebControls.CheckBoxList list)
{
    ArrayList values = new ArrayList();
    for(int counter = 0; counter < list.Items.Count; counter++)
    {
        if(list.Items[counter].Selected)
        {
            values.Add(list.Items[counter].Value);
        }    
    }
    return (String[]) values.ToArray( typeof( string ) );
 }

So, within your Page_Init event handler, call like so:

var selectedValues = CheckboxListSelections(myCheckBoxList);

Where myCheckBoxList is a reference to your CheckBoxList control.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • I asked using Request.Form not this. The reason being that on Page_Init that's the only way I can find out what check box items were selected. – The Light Jun 13 '11 at 11:19
  • Why can't you directly access the control from the `Page_Init` event handler? – Grant Thomas Jun 13 '11 at 12:42
  • like I said, the selected value is rendered after the Page_LoadViewState event so it doesn't exist in the Page_Init unless you use Request.Form. – The Light Jun 15 '11 at 11:29
0

I wrote this method which works but not with the best performance:

public static TimeOfDay Create(NameValueCollection httpRequestForm, string checkBoxId)
        {
            var result = new TimeOfDay();

            var selectedCheckBoxItems = from key in httpRequestForm.AllKeys
                       where key.Contains(checkBoxId)
                       select httpRequestForm.Get(key);

            if (selectedCheckBoxItems.Count() == 0)
            {
                result.ShowFull = true;
                return result;
            }

            foreach (var item in selectedCheckBoxItems)
            {
                var selectedValue = int.Parse(item.Substring(item.Length));

                    switch (selectedValue)
                    {
                        case 0:
                            result.ShowAm = true;
                            break;
                        case 1:
                            result.ShowPm = true;
                            break;
                        case 2:
                            result.ShowEvening = true;
                            break;
                        case 3:
                            result.ShowFull = true;
                            break;
                        default:
                            throw new ApplicationException("value is not supported int the check box list.");
                    }
                }

            return result;
        }

and use it like this:

TimeOfDay.Create(this.Request.Form, this.cblTimeOfDay.ID)
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
The Light
  • 26,341
  • 62
  • 176
  • 258