Thursday 25 August 2016

How to Enable and Disable all Validations Controls in a single page in Asp.net C#

For Disable validation controls in a page
public void DisableAllValidations(ControlCollection cntrls)
{
     foreach (Control ctrl in cntrls)
     {
        if (ctrl is CompareValidator)
            ((CompareValidator)ctrl).Enabled = false;
        if (ctrl is CustomValidator)
            ((CustomValidator)ctrl).Enabled = false;
        if (ctrl is RangeValidator)
            ((RangeValidator)ctrl).Enabled = false;
        if (ctrl is RegularExpressionValidator)
            ((RegularExpressionValidator)ctrl).Enabled = false;
        if (ctrl is RequiredFieldValidator)
            ((RequiredFieldValidator)ctrl).Enabled = false;
        DisableForm(ctrl.Controls);
     }
}
For Enable validation controls in a page
public void DisableAllValidations(ControlCollection cntrls)
{
     foreach (Control ctrl in cntrls)
     {
        if (ctrl is CompareValidator)
            ((CompareValidator)ctrl).Enabled = true;
        if (ctrl is CustomValidator)
            ((CustomValidator)ctrl).Enabled = true;
        if (ctrl is RangeValidator)
            ((RangeValidator)ctrl).Enabled = true;
        if (ctrl is RegularExpressionValidator)
            ((RegularExpressionValidator)ctrl).Enabled = true;
        if (ctrl is RequiredFieldValidator)
            ((RequiredFieldValidator)ctrl).Enabled = true;
        DisableForm(ctrl.Controls);
     }
}

Calculate the duration of the visit based on the Start and End Date. The duration will only count regular business days (does not count weekends or holidays)

private double GetBusinessDays(DateTime startD, DateTime endD)
{
   double calcBusinessDays =
           1 + ((endD - startD).TotalDays * 5 -
               (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;

   if (endD.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
   if (startD.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;
   return calcBusinessDays;
}