Hablitar and dehablilitar all the TextBox of a page

2

I have a page aspx where I have more than 200 TextBox , it's like a Form, then you have two options when you enter the page one of only "Show" and the other is "Edit", the options are enabled with two CheckBox :

protected void Page_Load(object sender, EventArgs e) {

    if ((CheckBoxMostrar.Checked == true))
    {
        CheckFalse();
    }
}

Event of CheckFalse() :

protected void Page_Load(object sender, EventArgs e) {

      public void CheckFalse ()  {
            //No es broma son como 200,sólo pongo 6
            Textbox1.Text= false;Textbox2.Text= false;Textbox3.Text= false;
            Textbox4.Text= false;Textbox5.Text= false; Textbox6.Text= false;
      }
}

The question is: How to enable and disable all of a single shot? No need to put everything.

    
asked by CarlosR93 31.01.2017 в 18:56
source

2 answers

4

If the textbox is on the page, you can help with linq using

public void CheckFalse()  
{
    foreach(var txt in this.Controls.OfType<TextBox>())
    {
        txt.Enabled = false;
    }
}

with the OfType<> you can find on the Controls collection all of a specific type

    
answered by 31.01.2017 в 19:33
0

An example method to enable / disable all WebControls or HtmlGenericControls from a parent control (can be the entire page this.Page or a specific control of the page and its children controls, a fieldset , div container, panel , etc ...)

    public static void RecursivoModoControles(Control control, bool enabled)
    {
        // Validar argumentos 
        if (control == null)
           return;

        // ¿Es un WebControl?
        WebControl webControl = control as WebControl;
        if (webControl != null)
            webControl.Enabled = enabled;

        // ¿Es un HtmlControl?  
        HtmlControl htmlControl = control as HtmlControl;
        if (htmlControl != null)
                htmlControl.Disabled = !enabled;

        // Tratar los controles hijos de forma recursiva
        foreach (Control child in control.Controls)
            RecursivoModoControles(child, enabled);
    }
    
answered by 01.02.2017 в 11:27