Search code examples
coding-stylewebforms

How to apply code to multiple controls at once?


Im new at ASP.net. I hope you can tell me the answer about this question.

I have a problem:

        if (Convert.ToDouble(txtAnalyseren.Text) > 5.5)
        {
            enough++;
        }

I have 7 of these more with all different text-boxes, I need to control if it is a enough or a "notenough" Is there a way to controle this at once? I don't want to copy and paste this for 7 times.


Solution

  • One way could be cycling through all of your controls in the page and detect which of them is a textbox:

    foreach (Control c in this.Controls)
    {
        if (c is TextBox)
        {
            ....
        }
    }