0

I need to change the visibility of many controls in my aspx page.

I found several methods to get the controls like this one

But I can't set values to this controls because they are passed by val, and I don't figure out how can I add the ref key word in this case.

Community
  • 1
  • 1
ihebiheb
  • 3,673
  • 3
  • 46
  • 55
  • 2
    `Control` is a reference type so a reference is passed. You should have no problem setting the visibility property. – Magnus Nov 14 '13 at 14:22

2 Answers2

2

Try this link, it should work fine. By the way, control is a reference type not a value type.

Community
  • 1
  • 1
Oualid KTATA
  • 1,116
  • 10
  • 20
1

From the example in your question, do this:

IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        yield return child;
        foreach (Control descendant in EnumerateControlsRecursive(child))
            yield return descendant;
    }
}

Usage:

foreach (Control c in EnumerateControlsRecursive(Page))
{
    if(c is TextBox)
    {
        var theTextBox = c as TextBox;
        theTextBox.Visible = false;
    }

    if(c is Label)
    {
        var theLabel = c as Label;
        theLabel.Visible = false;
    }

    ...
}
Community
  • 1
  • 1
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80