How to know which value items where selected from a CheckBoxList using Request.Form?
Asked Answered
S

2

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,

Suzannesuzerain answered 13/6, 2011 at 11:2 Comment(0)
S
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)
Suzannesuzerain answered 15/6, 2011 at 11:30 Comment(0)
M
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.

Mannino answered 13/6, 2011 at 11:8 Comment(3)
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.Suzannesuzerain
Why can't you directly access the control from the Page_Init event handler?Mannino
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.Suzannesuzerain
S
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)
Suzannesuzerain answered 15/6, 2011 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.