Wednesday, November 16, 2011

Enable or disable all controls in the page-C#

Using the code

ChangeControlStatus(false) ;

The Method ChangeControl status accepts a boolean parameter .The parameter value can be set to True/False. When the Parameter value is false all the Controls are disabled and vice versa.
Collapse | Copy Code

private void ChangeControlStatus(bool status)
{

foreach (Control c in Page.Controls)
foreach (Control ctrl in c.Controls)

if (ctrl is TextBox)

((TextBox)ctrl).Enabled = status;

else if (ctrl is Button)

((Button)ctrl).Enabled = status;

else if (ctrl is RadioButton)

((RadioButton)ctrl).Enabled = status;

else if (ctrl is ImageButton)

((ImageButton)ctrl).Enabled = status;

else if (ctrl is CheckBox)

((CheckBox)ctrl).Enabled = status;

else if (ctrl is DropDownList)

((DropDownList)ctrl).Enabled = status;

else if (ctrl is HyperLink)

((HyperLink)ctrl).Enabled = status;

}
private void ClearControls()
{
foreach(Control c in Page.Controls)
{
foreach (Control ctrl in c.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Text = string.Empty;
}
}
}
}

Calling the Method

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

ChangeControlStatus(false);
}

No comments:

Post a Comment