2

My requirement is to count the total number of TextBox and CheckBox present directly inside the form with the id="form1", when the user clicks on the Button 'btnGetCount'. Here is the code which I have tried but it doesn't count anything and counter remains at zero, though I have three TextBoxes and two CheckBoxes in the form. However, if I remove foreach loop and pass TextBox control = new TextBox(); instead of the present code then it counts the first TextBox and countTB returns the value as one.

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);
    }
RahulD
  • 709
  • 2
  • 16
  • 39

4 Answers4

4

It allows gives you zero since you are counting the type of your Control control which is not available change your code to :

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);
    } 
Alyafey
  • 1,455
  • 3
  • 15
  • 23
  • Yes, I tried to point this out in my answer. Don't know why I was downvoted. Tried the OP's code and it outputted 0. Tried it the way you and I posted it and it worked just fine. – pcnThird Jul 17 '13 at 23:51
  • pcnThird Did you make any other changes? Its still giving me the same result. PS: I didn't downvote you. – RahulD Jul 17 '13 at 23:54
  • Well in the `else if` statement I used the same condition as you posted, but other than that, it's pretty much the same. – pcnThird Jul 17 '13 at 23:56
  • Thanks a lot for the answer guys. I made a minor change in your and Alyafey code i.e. form1.Controls instead of this.Controls and it works fine. Thanks again. PS: pcnThird you deleted your answer, had to make it +1. – RahulD Jul 17 '13 at 23:59
4

You must recursively loop through other controls.

    <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);
    }
}
Valamas
  • 24,169
  • 25
  • 107
  • 177
  • Tried this code, it works fine for TextBoxes but adds one extra count for CheckBox. I'll debug that. +1 :) – RahulD Jul 18 '13 at 00:13
  • I got that, it was counting radiobuttons also as checkboxes because of inheritence, so by replacing 'is' with GetType() and typeOf() logic, it works fine. – RahulD Jul 18 '13 at 18:44
0

I believe you have to be recursive, this.Controls will only return the controls that are its immediate children. If the TextBox is within a control group within there, you will need to see the containers controls as well.

See the answer to this other stackoverflow: How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

EDIT: I realize the answer is for WinForms, but the solution still applies.

Community
  • 1
  • 1
Matthew
  • 24,703
  • 9
  • 76
  • 110
0

Just by doing some minor change in the code suggested by Alyafey, pcnThird and Valamas I wrote this code which works.

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);
    } 
RahulD
  • 709
  • 2
  • 16
  • 39
  • So what was the result ? was is it the needed change ? – Alyafey Jul 18 '13 at 00:21
  • Instead of this.Controls, I made that form1.Controls.. :) – RahulD Jul 18 '13 at 18:00
  • @Alyafey I made that change in your code, not in mine. One error had been removed by you already by replacing control with 'c'. It is now counting the number of TextBoxes and CheckBoxes perfectly. – RahulD Jul 18 '13 at 18:41