2

id="form1"我的要求是当用户单击 Button 时,直接在表单中计算 TextBox 和 CheckBox 的总数'btnGetCount'。这是我尝试过的代码,但它不计算任何东西并且计数器保持为零,尽管我在表单中有三个文本框和两个复选框。但是,如果我删除 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

4 回答 4

4

它允许给您零,因为您正在计算不可用的 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);
    } 
于 2013-07-17T23:45:57.507 回答
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);
    }
}
于 2013-07-17T23:55:58.073 回答
0

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

请参阅其他 stackoverflow 的答案:如何获取特定类型(按钮/文本框)的 Windows 窗体窗体的所有子控件?

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

于 2013-07-17T23:36:19.270 回答
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);
    } 
于 2013-07-18T00:11:29.350 回答