Friday, September 11, 2009

Disable All the Controls in ASP.Net Web Forms using C#.net

To disable all the controls(label,textbox,dropdown etc) in a web form use the following function

Call the below function like: SetControlsDisable(Page);

public void SetControlsDisable(Control Page)
{
foreach (Control ctrl in Page.Controls)
{
if (ctrl is TextBox)
{
((TextBox)(ctrl)).Enabled = false;
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControlsDisable(ctrl);
}
}

if (ctrl is Button)
{
((Button)(ctrl)).Enabled = false;
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControlsDisable(ctrl);
}
}

if (ctrl is DropDownList)
{
((DropDownList)(ctrl)).Enabled = false;
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControlsDisable(ctrl);
}
}


if (ctrl is CheckBoxList)
{
((CheckBoxList)(ctrl)).Enabled = false;
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControlsDisable(ctrl);
}
}

if (ctrl is ImageButton)
{
((ImageButton)(ctrl)).Enabled = false;
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControlsDisable(ctrl);
}
}

if (ctrl is CheckBox)
{
((CheckBox)(ctrl)).Enabled = false;
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControlsDisable(ctrl);
}
}


if (ctrl is LinkButton)
{
((LinkButton)(ctrl)).Enabled = false;
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControlsDisable(ctrl);
}
}
}
}

No comments:

Post a Comment