欢迎访问我的博客 https://javascript.shop

遍历页面所有的Checkbox,显示选中的ID

原文发布时间为:2009-04-13 —— 来源于本人的百度文章 [由搬家工具导入]

1、

foreach (Control objCtrl in this.Page.Controls[3].Controls)
        {
            if (objCtrl is CheckBox)
            {
                if (((CheckBox)objCtrl).Checked == true)
                    Response.Write(objCtrl.ClientID.ToString() + "<br>");
            }
        }

这种代码没有通用性,你怎么就知道Control[3]正是你要遍历的collection

2、通用性代码

调用函数: InitialControl(this.Page.Controls);

private void InitialControl(ControlCollection objControlCollection)
    {
        foreach (System.Web.UI.Control objControl in objControlCollection)
        {
            if (objControl.HasControls())
            {
                InitialControl(objControl.Controls);
            }
            else
            {
                if (objControl is CheckBox)
                {
                    if (((CheckBox)objControl).Checked == true)
                        Response.Write(objControl.ClientID.ToString()+"<br>");
                }
            }
        }
    }

posted @ 2017-07-12 00:05  孑孓子  阅读(240)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop