Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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;
    }

    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top