Programatically Create DatePicker

5 posts, 1 answers
  1. Mike
    Mike avatar
    74 posts
    Member since:
    Mar 2013

    Posted 26 Jul Link to this post

    I have the following code to programmatically create a DatePicker

    case "date":
        var dt = new RadDatePicker { ID = $"keyword_{keyword.keyword_id}" };
        var day = new RadCalendarDay
        {
          Repeatable = RecurringEvents.Today,
          Date = DateTime.Today
        };
        day.ItemStyle.BackColor = Color.LightGray;
        day.ItemStyle.Font.Bold = true;
        day.ItemStyle.BorderStyle = BorderStyle.Solid;
        day.ItemStyle.BorderColor = Color.Black;
        day.ItemStyle.BorderWidth = new Unit(1);
        if (dt.SharedCalendar == null)
        {
          dt.SharedCalendar = new RadCalendar();
        }
        dt.SharedCalendar.SpecialDays.Add(day);
        cell1.Controls.Add(lbl);
        cell2.Controls.Add(dt);
        break;

    and I receive the following on the page when I try to view it:

    Value cannot be null or empty.
    Parameter name: componentID

    I've got it narrowed down to the SharedCalendar not having an ID value, and when I set it to some value, I now get an error in the JS console:

    Uncaught Sys.InvalidOperationException: Sys.InavalidOperationException: Compnoent 'cal_keyword_134' was not found

    How can I create a Date Picker programmatically and have the popup calendar highlight "today"?

     

  2. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 29 Jul Link to this post

    Hello Mike,

    Check out the code snippets below that illustrate how you can add a RadDatePicker control programmatically. Give the approach a try and see how it works for you.

    Markup:

    <asp:PlaceHolder ID="PlaceHolder1" runat="server" />

    Code-behind:


    protected void Page_Init(object sender, EventArgs e)
    {
        RadDatePicker datePicker = new RadDatePicker();
        datePicker.ID = "DatePicker1";
     
        RadCalendarDay day = new RadCalendarDay();
        day.Repeatable = RecurringEvents.Today;
        day.Date = DateTime.Today;
        day.ItemStyle.BackColor = System.Drawing.Color.Red;
         
        datePicker.Calendar.SpecialDays.Add(day);
     
        PlaceHolder1.Controls.Add(datePicker);
     
    }




    Regards,
    Viktor Tachev
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mike
    Mike avatar
    74 posts
    Member since:
    Mar 2013

    Posted 29 Jul in reply to Viktor Tachev Link to this post

    Ok, that worked.  I see you added the RadCalendarDay to the Calendar property of the DatePicker, not the SharedCalendar.  I remember reading a forum post a while ago and it said to attach the RadCalendarDay to the SharedCalendar - but I can't find the post again.
  5. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 03 Aug Link to this post

    Hi Mike,

    If the RadDatePicker is using a SharedCalendar you would need to configure the settings of the shared calendar. Otherwise you should configure the Calendar that is rendered for the current picker control.

    If you would like additional information on using SharedCalendar you would find the following article interesting.



    Regards,
    Viktor Tachev
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  6. Mike
    Mike avatar
    74 posts
    Member since:
    Mar 2013

    Posted 03 Aug in reply to Viktor Tachev Link to this post

    Awesome, thanks!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017