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

ぐ巨炮叔叔 提交于 2020-01-04 05:28:47

问题


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,


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/6329552/how-to-know-which-value-items-where-selected-from-a-checkboxlist-using-request-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!