This is a migrated thread and some comments may be shown as answers.

Dynamically generated RadDatePicker controls not triggering the Compare Validator.

1 Answer 20 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Naeem
Top achievements
Rank 1
Naeem asked on 19 Aug 2015, 06:55 AM

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.

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 20 Aug 2015, 02:47 PM
Hi Naeem,

Make sure that you are adding the picker controls and the Validator early enough in the Page lifecycle. For convenience I have prepared a sample project. It is working as expected on my end. Try to use similar approach and let me know how it works for you.


Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
DatePicker
Asked by
Naeem
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or