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

Dynamically created controls in RadCalendar

9 Answers 270 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
John Mann
Top achievements
Rank 1
John Mann asked on 16 Jun 2009, 04:29 PM

I have a user control that I dynamically create in the cells of a radcalendar. The control is based on the results of a database query that takes place in the DayRender event for each day of the calendar. Any given day can have zero, none or many of the user controls created within it. All this works fine, the problem is that none of the events associated with the user control ever get called when triggered. For instance if a checkbox is clicked the page will postback, but the event for this checkbox in the usercontrol doesn't execute. I know why this is, but I don't know how to fix it. Normally I would create/recreate my user controls in OnInit, but since this is happening in DayRender the controls don't exist when the page posts back. How can I get this to work?

Thank you,

John

(I had mistakenly posted this in the non-Ajax forum previously)

9 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 19 Jun 2009, 02:13 PM
Hi John Mann,

You are correct in observing that the reason for the events not being fired is due to the fact that when the page life cycle reaches the stage at which post back events are raised, the user controls have not been recreated yet. Unfortunately, this scenario is not possible to achieve on account of the above sequence of events.

I hope this information helps.

Kind regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
John Mann
Top achievements
Rank 1
answered on 19 Jun 2009, 03:32 PM
Actually it is possible. I was able to do it using the ITemplate Interface. I create a class implementing an ITemplate then in the OnInit event I loop through all the days of the calendar and call that class to add the necessary controls to each day. I don't have the code with me right now, but can post it if anyone is interested. It turned out to be pretty simple, just took a long time to figure it out.
0
Sebastian
Telerik team
answered on 24 Jun 2009, 02:29 PM
Hello John,

I am glad that you discovered a solution which suits your needs. We will appreciate if you post it in this public forum thread in order to make it available for other community members as well. As a token of gratitude we will add some Telerik points to your account which can be used as a discount for future upgrades/purchases of our controls.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
John Mann
Top achievements
Rank 1
answered on 24 Jun 2009, 09:59 PM
Okay, here it is. I'm still figuring out how this works, so I'm sure there is lots of room for improvement. Any suggestions are welcome.

John

aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestApp.WebForm1" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
  <title></title>  
</head> 
<body> 
  <form id="form1" runat="server">  
  <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
  </telerik:RadScriptManager> 
  <div> 
    <telerik:RadCalendar ID="RadCalendar1" runat="server" Font-Names="Arial,Verdana,Tahoma" ForeColor="Black" Style="border-color: #ececec" DayNameFormat="Short" EnableViewState="False" PresentationType="Preview">  
    </telerik:RadCalendar> 
  </div> 
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
  </form> 
</body> 
</html> 
 

Code behind:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Telerik.Web.UI;  
 
namespace TestApp  
{  
  public partial class WebForm1 : System.Web.UI.Page  
  {  
    protected override void OnInit(EventArgs e)  
    {  
      int someData = 0;  
      DateTime rcDate = RadCalendar1.CalendarView.ViewStartDate;  
      while (rcDate <= RadCalendar1.CalendarView.ViewEndDate)  
      {  
        // someData could be a useful bit of information or even a control that needs to be added to a day cell  
        CalendarDayTemplate template = new CalendarDayTemplate(RadCalendar1, rcDate, someData);  
        rcDate = rcDate.AddDays(1.0);  
        someData++;  
      }  
 
      // this is optional, sets the style of the sells programmatically  
      RadCalendar1.DayRender += new Telerik.Web.UI.Calendar.DayRenderEventHandler(RadCalendar1_DayRender);  
      
      base.OnInit(e);  
    }  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
 
    void RadCalendar1_DayRender(object sender, Telerik.Web.UI.Calendar.DayRenderEventArgs e)  
    {  
      TableCell myCell = e.Cell;  
      myCell.Style.Add(HtmlTextWriterStyle.VerticalAlign, "Top");  
      myCell.BackColor = System.Drawing.Color.White;  
      myCell.BorderColor = System.Drawing.ColorTranslator.FromHtml("#e0e0e0");  
    }  
  }  
 
  public class CalendarDayTemplate : ITemplate  
  {  
    private Control cellContent = new Control();  
    public CalendarDayTemplate(RadCalendar myCalendar, DateTime cellDate, int myData)  
    {  
      RadCalendarDay newDay = new RadCalendarDay();  
      PlaceHolder myPlaceHolder = new PlaceHolder();  
      Page myPage = new Page();  
 
      Label lblDate = new Label();  
      lblDate.Style.Add(HtmlTextWriterStyle.TextAlign, "center");  
      lblDate.Width = Unit.Pixel(80);  
      lblDate.BackColor = System.Drawing.Color.AliceBlue;  
      lblDate.Text = cellDate.ToString("M/d");  
      myPlaceHolder.Controls.Add(lblDate);  
 
      // Dynamically add whatever controls you need here. Possibly based on values read from a database.  
      // If only one control is being added to a cell the PlaceHolder isn't necessary  
      for (int counter = 0; counter < 3; counter++)  
      {  
        LiteralControl br = new LiteralControl("<br />");  
        TextBox myTextBox = new TextBox();  
        myTextBox.Text = "" + myData;  
        myTextBox.Width = Unit.Pixel(30);  
        myTextBox.AutoPostBack = true;  
        myTextBox.TextChanged += new EventHandler(myTextBox_TextChanged);  
        myTextBox.ID = "mtb" + myData + "-" + counter;  
 
        myPlaceHolder.Controls.Add(br);  
        myPlaceHolder.Controls.Add(myTextBox);  
      }  
      this.cellContent = myPlaceHolder;  
 
      // Create a DayTemplate with a unique ID  
      DayTemplate dayTemplate = new DayTemplate();  
      dayTemplate.ID = cellDate.ToString("MMddyyyy");  
      dayTemplate.Content = this;  
 
      // create a SpecialDay, associate it with the DayTemplate and add to Calendar  
      newDay.Date = cellDate;  
      newDay.TemplateID = dayTemplate.ID;  
      myCalendar.SpecialDays.Add(newDay);  
 
      myCalendar.CalendarDayTemplates.Add(dayTemplate);  
    }  
 
    // Once the RadCalendar is instantiated this will be called once for each cell
    public void InstantiateIn(Control container)  
    {  
      container.Controls.Add(this.cellContent);  
    }  
 
    protected void myTextBox_TextChanged(object sender, EventArgs e)  
    {  
      // do something here based on user input  
    }  
  }  
}  
 
0
Sebastian
Telerik team
answered on 25 Jun 2009, 07:43 AM
Hello John,

Thank you for sharing your implementation with the Telerik community. I updated your Telerik points for the involvement.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Eric
Top achievements
Rank 1
answered on 18 May 2012, 02:10 PM
This is a really good technique, but unfortuantely only works for the current view. If you switch months, those cells will not be templated.
0
Eric
Top achievements
Rank 1
answered on 21 May 2012, 09:52 PM
I can't get events to work with this (using John's example). I have AutoPostBack set to true on both the Textbox and Calendar.
0
Maria Ilieva
Telerik team
answered on 23 May 2012, 08:19 AM
Hello Eric,

As we are already discussing this issue in the support ticket you have opened with us I will go ahead and close this forum thread. Thus we will continue our communication in the other support thread for better tracking the issues you are facing.

All the best,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 28 Aug 2020, 09:57 PM

Attila, help me a lot this this code:

 

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

ASPX.cs:

protected void Page_Init(object sender, EventArgs e)
{
    var calendar = new RadCalendar() { ID = "RadCalendar1" };

    var newDayTemplate = new DayTemplate() { ID="MyDayTemplate" };

    newDayTemplate.Content = new CustomDayTemplate();

    calendar.CalendarDayTemplates.Add(newDayTemplate);


    RadCalendarDay NewDay = new RadCalendarDay(calendar);
    NewDay.Date = DateTime.Now.Date;
    NewDay.Repeatable = Telerik.Web.UI.Calendar.RecurringEvents.DayAndMonth;
    NewDay.TemplateID = "MyDayTemplate";
    NewDay.ToolTip = "My custom Template";

    calendar.SpecialDays.Add(NewDay);

    PlaceHolder1.Controls.Add(calendar);
}

class CustomDayTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {

        var lbl = new Label() { ID = "Label1" };

        lbl.DataBinding += Lbl_DataBinding;

        container.Controls.Add(lbl);
    }

    private void Lbl_DataBinding(object sender, EventArgs e)
    {
        var lbl = (Label)sender;

        lbl.Text = "MyDay";
    }
}

#jefferson2020

Tags
Calendar
Asked by
John Mann
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
John Mann
Top achievements
Rank 1
Sebastian
Telerik team
Eric
Top achievements
Rank 1
Maria Ilieva
Telerik team
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or