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

Programatically Create DatePicker

4 Answers 134 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 26 Jul 2016, 09:07 PM

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"?

 

4 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 29 Jul 2016, 11:58 AM
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.
0
Mike
Top achievements
Rank 1
answered on 29 Jul 2016, 02:56 PM
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.
0
Viktor Tachev
Telerik team
answered on 03 Aug 2016, 01:10 PM
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.
0
Mike
Top achievements
Rank 1
answered on 03 Aug 2016, 02:34 PM
Awesome, thanks!
Tags
DatePicker
Asked by
Mike
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Mike
Top achievements
Rank 1
Share this question
or