计算表单中TextBox和CheckBoxes的数量

时间:2013-07-17 23:33:04

标签: c# asp.net checkbox textbox count

当用户点击按钮id="form1"时,我的要求是使用'btnGetCount'计算表单内直接存在的TextBox和CheckBox的总数。这是我尝试过的代码但是它没有计算任何东西,并且计数器保持为零,尽管我在表单中有三个TextBox和两个CheckBox。但是,如果我删除foreach循环并传递TextBox control = new TextBox();而不是当前代码,则它会计算第一个TextBox,countTB将该值返回为一个。

protected void btnGetCount_Click(object sender, EventArgs e)
    {
        Control control = new Control();

        int countCB = 0;
        int countTB = 0;
        foreach (Control c in this.Controls)
        {
            if (control.GetType() == typeof(CheckBox))
            {
                countCB++;
            }
            else if (control is TextBox)
            {
                countTB++;
            }
        }

        Response.Write("No of TextBoxes: " + countTB);
        Response.Write("<br>");
        Response.Write("No of CheckBoxes: " + countCB);
    }

4 个答案:

答案 0 :(得分:4)

您必须递归遍历其他控件。

    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    <asp:CheckBox ID="cb" runat="server"></asp:CheckBox>
    </div>
    </form>

protected void Page_Load(object sender, EventArgs e)
{
    var controls = form1.Controls;
    var tbCount = 0;
    var cbCount = 0;
    CountControls(ref tbCount, controls, ref cbCount);

    Response.Write(tbCount);
    Response.Write(cbCount);
}

private static void CountControls(ref int tbCount, ControlCollection controls, ref int cbCount)
{
    foreach (Control wc in controls)
    {
        if (wc is TextBox)
            tbCount++;
        else if (wc is CheckBox)
            cbCount++;
        else if(wc.Controls.Count > 0)
            CountControls(ref tbCount, wc.Controls, ref cbCount);
    }
}

答案 1 :(得分:3)

它允许您为零,因为您计算的Control控件的类型不可用,请将您的代码更改为:

protected void btnGetCount_Click(object sender, EventArgs e)
    {

        int countCB = 0;
        int countTB = 0;
        foreach (Control c in this.Controls)
        {
            if (c.GetType() == typeof(CheckBox))
            {
                countCB++;
            }
            else if (c.GetType()== typeof(TextBox))
            {
                countTB++;
            }
        }

        Response.Write("No of TextBoxes: " + countTB);
        Response.Write("<br>");
        Response.Write("No of CheckBoxes: " + countCB);
    } 

答案 2 :(得分:0)

我相信你必须是递归的,this.Controls只会返回其直接子节点的控件。如果TextBox在其中的控件组内,您还需要查看容器控件。

请参阅此其他stackoverflow的答案:How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

编辑: 我意识到答案是针对WinForms的,但解决方案仍然适用。

答案 3 :(得分:0)

只需对Alyafey,pcnThird和Valamas建议的代码做一些小改动,我就编写了这个有效的代码。

protected void btnGetCount_Click(object sender, EventArgs e)
    {

        int countCB = 0;
        int countTB = 0;
        foreach (Control c in form1.Controls) //here is the minor change
        {
            if (c.GetType() == typeof(CheckBox))
            {
                countCB++;
            }
            else if (c.GetType()== typeof(TextBox))
            {
                countTB++;
            }
        }

        Response.Write("No of TextBoxes: " + countTB);
        Response.Write("<br>");
        Response.Write("No of CheckBoxes: " + countCB);
    }