如何计算asp.net形式的复选框 [英] How to count the checkboxes in a asp.net form

查看:54
本文介绍了如何计算asp.net形式的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET:

<asp:Panel ID="pnlFilter" runat="server">
    <div class="dvFilter">
        <input type="checkbox" id="cb01" checked="checked" />
        <label for="cb01">All</label>
    </div>
    <div class="dvFilter">
        <input type="checkbox" id="cb02" checked="checked" />
        <label for="cb02">None</label>
    </div>
</asp:Panel>

C#:

foreach (Control item in this.form1.Controls)
{
    System.Web.UI.HtmlControls.HtmlInputCheckBox _cbx = item as System.Web.UI.HtmlControls.HtmlInputCheckBox;
    if (_cbx != null)
    {
        if (_cbx.Checked)
        {
            //Do something: 
            Response.Write(_cbx.Name + " was checked.<br />");
        }
    }

}

我是为 _cbx 变量获取 null 值。

如何更新它,以便能够获取所有选中的输入类型复选框的ID。

How can I update it so I am able to get the ID of all the checked input type checkboxes.

I尝试了以下答案:计算文本框和复选框中的数量表单,但对我也不起作用。

I tried this answer: Count the number of TextBoxes and CheckBoxes in a form but didn't work for me either.

推荐答案

如果要解析DOM,则需要解析使用<输入类型= checkbox>


我建议您使用< asp: CheckBox> 而不是< input type = checkbox> 。然后,您可以从c#中访问控件



在您的查找中:


var _cbx = item作为System。 Web.UI.WebControls.CheckBox;


您可以获取所有复选框,而无需循环浏览所有控件。
使用LINQ:


this.form1.Controls.Where(c => c.GetType()== typeof(CheckBox))

You need to parse the DOM if you want to use <input type="checkbox">.

I advise you to use <asp:CheckBox> instead of <input type="checkbox">. Then you can access your controls from c#

In your lookup:
var _cbx = item as System.Web.UI.WebControls.CheckBox;

You can get all checkboxes without looping through all controls.
Use LINQ:
this.form1.Controls.Where(c=>c.GetType() == typeof(CheckBox))

这篇关于如何计算asp.net形式的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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