Enalbe Or Disable Controls In Asp.Net

Give ID and runat=”server” attribute to Div or Table Tag for enable or disable the controls in it. Write the followin function and call them on any event.

Disable Controls

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;
}
if (_ChildControl is RadioButton)
{
((RadioButton)_ChildControl).Enabled = false;
}
if (_ChildControl is GridView)
{

((GridView)_ChildControl).Enabled = false;
}
if (_ChildControl is LinkButton)
{
((LinkButton)_ChildControl).Enabled = false;
}
}
}
}
Enable Contorls
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;
}
if (_ChildControl is RadioButton)
{
((RadioButton)_ChildControl).Enabled = true;
}
if (_ChildControl is GridView)
{
((GridView)_ChildControl).Enabled = true;
}
if (_ChildControl is LinkButton)
{
((LinkButton)_ChildControl).Enabled = true;
}
}
}

}
#endregion

#region “Update and Get Boq Approve details”
public void UpdateApproveByName()
{
dtSky = (DataTable)ViewState[“dtSky”];
SortedList sl = new SortedList();
sl.Add(“@ApproveBy”, txtApprovedName.Text);
sl.Add(“@ApproveDate”, DateTime.Today.ToShortDateString());
sl.Add(“@BoqNo”, Convert.ToString(dtSky.Rows[0][“BoqNo”]));

int rowAffected = obj.ExecuteSQL(“spUpdateBoq”, sl);
}

public void GetApproveDetails()
{
if (!string.IsNullOrEmpty(Request.QueryString[“BoqNo”]))
{
DataTable dtBoq = new DataTable();
SortedList sl = new SortedList();
sl.Add(“@BoqNo”,Convert.ToString(Request.QueryString[“BoqNo”]));
dtBoq = obj.GetDataTable(“spGetBoqId”,sl);
if (dtBoq != null)
{
if (dtBoq.Rows.Count > 0)
{
txtApprovedName.Text = Convert.ToString(dtBoq.Rows[0][“ApproveBy”]);
if (dtBoq.Rows[0][“ApproveDate”] != null)
{
lblApproveDateVal.Text = Convert.ToString(dtBoq.Rows[0][“ApproveDate”]);
}
}
}
}
}

Share