Dynamically generated RadDatePicker controls not triggering the Compare Validator.

2 posts, 0 answers
  1. Naeem
    Naeem avatar
    1 posts
    Member since:
    Dec 2011

    Posted 19 Aug 2015 Link to this post

    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.

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 20 Aug 2015 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top