Hi,
I have a problem with RadDatePicker controls in Asp.Net.
I am dynamically creating 2 RadDatePicker controls at runtime and I want to apply CompareValidator to check if the 'From Date' is less than the 'To Date'. The controls are dynamically created so i have set the AutoPostBack = false. With that the controls are not throwing the validation if i choose the wrong dates.
The condition is:
Date Validation checks - From Date <= To date
Here is my code:
for (int j = 0; j < slistArray.Length; j++)
{
int counter = 0;
int DateRangeFound = 0;
switch (QuestionType)
{
case "DateRange":
if (QuestionName == "DateFrom")
{
RadDatePicker rdpDateFrom = new RadDatePicker();
rdpDateFrom.CssClass = "form-control form-control-item";
rdpDateFrom.ID = "rdpDateFrom" + j.ToString() + "-" + counter;
rdpDateFrom.AutoPostBack = false;
rdpDateFrom.DatePopupButton.Visible = true;
rdpDateFrom.ShowPopupOnFocus = true;
rdpDateFrom.EnableScreenBoundaryDetection = true;
rdpDateFrom.MaxDate = DateTime.Now;
txtC1.Controls.Add(rdpDateFrom);
++DateRangeFound;
}
else if (QuestionName == "DateTo")
{
RadDatePicker rdpDateTo = new RadDatePicker();
rdpDateTo.CssClass = "form-control form-control-item";
rdpDateTo.ID = "rdpDateTo" + j.ToString() + "-" + (counter - 1);
rdpDateTo.AutoPostBack = false;
rdpDateTo.DatePopupButton.Visible = true;
rdpDateTo.ShowPopupOnFocus = true;
rdpDateTo.EnableScreenBoundaryDetection = true;
rdpDateTo.MaxDate = DateTime.Now;
txtC1.Controls.Add(rdpDateTo);
++DateRangeFound;
}
if (DateRangeFound > 1)
{
CompareValidator cvdpDate = new CompareValidator();
cvdpDate.ID = "cvdpDate" + j.ToString() + "-" + counter;
cvdpDate.ControlToCompare = "rdpDateFrom" + j.ToString() + "-" + (counter - 1);
cvdpDate.ControlToValidate = "rdpDateTo" + j.ToString() + "-" + (counter - 1);
cvdpDate.Operator = ValidationCompareOperator.GreaterThanEqual;
cvdpDate.Type = ValidationDataType.Date;
cvdpDate.Text = "*";
cvdpDate.ForeColor = Color.Red;
cvdpDate.ErrorMessage = "End date must be equal or greater than start date - please correct dates.";
txtC1.Controls.Add(cvdpDate);
}
break;
}
panelBody.Controls.Add(txtC1);
counter++;
}
Any suggestions for the client side jquery validation will also be helpful.
Thank you.