0

I have multiple TextBoxes set on enabled = false. If user clicks on Button (btnEdit) all the textboxes should get enabled = true.

My Code:

protected void Read(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox && c.ID.StartsWith("txt"))
                ((TextBox)c).Enabled = true;
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Read(pnlDialog);
    }

pnlDialog is a Panel with all the TextBoxs.

Olga
  • 117
  • 3
  • 9

1 Answers1

0

Their can be a problem in your code, I'm not very sure about it.Your code should not be able to function and meed your requirement. The code

((TextBox)c).Enabled = true

you used actually behaves in the following descripted way:
1.It creates a COPY of the object c.
2.It sets the 'Enabled' property of the COPY to true.
This means that it wont set the 'Enabled' propert of the origin(c in this case) to true.
What you shoud do is

TextBox a = c as TextBox;
a.Enabled=true;

This should be working.
Again, I'm not very sure about what I said.But you can always have a try.

  • And for the same reason, similar things can happen on
     foreach (Control c in control.Controls)
    The 'c' here may have already been a copy of the origin TextBox object,in this case,you should use for loop instead.
    – Ragnarokkr Xia Mar 29 '17 at 12:24