How to validate several empty txt?

0

Hi, I'm a student and I have the following problem. I am developing a form in C # and I would like to avoid some txt being empty, I have read that this is a way of doing it:

if (this.txt.Text.Equals(""))
    {
        Response.Write("El campo  esta vacio");
    }

But it turns out that I have several txt, there is some way to validate that they are not empty without using as many if, or I will have to do so. Thank you

    
asked by Kevin S 28.06.2018 в 05:44
source

1 answer

1

You could go through the controls that you have on your form, ask if that control is of the Text type and then control whether it was empty or not. I'm not with Visual Studio installed to test the code that happened to you, but that's the logic that I used.

foreach (Control c in this.Controls){
if (c is TextBox){
    TextBox textBox = c as TextBox;
    if (textBox.Text == string.Empty){
         Response.Write("El campo  esta vacio");
    }
  }
}
    
answered by 28.06.2018 в 07:00