如何知道使用Request.Form从CheckBoxList中选择了哪些值项目? [英] How to know which value items where selected from a CheckBoxList using Request.Form?

查看:96
本文介绍了如何知道使用Request.Form从CheckBoxList中选择了哪些值项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Request.Form从CheckBoxList中选择哪些值项?

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

我看到这两个表单键:

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

0和3是我的复选框列表中的选定值,其中包含4个项目。

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

我需要在Page_Init上以编程方式找到那些值

I'd need to find those values programmaticlaly on Page_Init

谢谢

推荐答案

我编写了此方法,该方法可以工作,但不能达到最佳性能:

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;
        }

并像这样使用它:

TimeOfDay.Create(this.Request.Form, this.cblTimeOfDay.ID)

这篇关于如何知道使用Request.Form从CheckBoxList中选择了哪些值项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆