Asp.Net Enable Disable And Clear All Control

Asp.Net Enable – Disable Control

To Enable All Controls Which is in Panel Or any Other Form Comtrol Call this Function

DisableControls(this.PnlEdit);

private void DisableControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
DisableControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Enabled = false;
}
else
if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Enabled = false;
}
if (_ChildControl is DropDownList)
{
((DropDownList)_ChildControl).Enabled = false;
}

}
}
}
private void EnableControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
EnableControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Enabled = true ;
}
else
if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Enabled = true;
}
if (_ChildControl is DropDownList)
{
((DropDownList)_ChildControl).Enabled = true;
}

}
}
}

———————————————————————————————-

Same way to Clear all Control

private void ClearControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
ClearControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Text = string.Empty;
}
else
if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Checked = false;
}

}
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s