Click to See Complete Forum and Search --> : Disable all Controls in a Page


hobbes
04-11-2005, 05:43 AM
Hello,

I generate dynamic controls in my web page. And one a certain condition (being met from Oracle table) when the page loads, I need to populate the dynamic controls and then disable them.

I have populated the fields, but disabling the controls is proving a little too difficult to me.

I am able to disable each control individually, but not all controls at once.

This is what I tried,

foreach(Control con in Page.Controls)
{
if(con is TextBox)
((TextBox) con).Enabled = false;
}

Hobbes

Phil Weber
04-11-2005, 01:06 PM
What happens when you run that code? Have you stepped through it in the debugger?

cmm
04-11-2005, 07:08 PM
Are the textboxes possibly children of other controls besides the Page object? Try this solution which recursively disables every textbox in the control tree:


private void DisableTextBoxes()
{
DisableTextBoxesRecursive(Page);
}

private void DisableTextBoxesRecursive(Control root)
{
if (root is TextBox)
{
((TextBox) root).Enabled = false;
}

foreach (Control child in root.Controls)
{
DisableTextBoxesRecursive(child);
}
}


Call DisableTextBoxes() to disable all textboxes in the page.