Dynamically created controls in RadCalendar

9 posts, 0 answers
  1. John Mann
    John Mann avatar
    34 posts
    Member since:
    Nov 2008

    Posted 16 Jun 2009 Link to this post

    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)

  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 19 Jun 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. John Mann
    John Mann avatar
    34 posts
    Member since:
    Nov 2008

    Posted 19 Jun 2009 Link to this post

    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.
  5. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 24 Jun 2009 Link to this post

    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.
  6. John Mann
    John Mann avatar
    34 posts
    Member since:
    Nov 2008

    Posted 24 Jun 2009 Link to this post

    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  
        }  
      }  
    }  
     
  7. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 25 Jun 2009 Link to this post

    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.
  8. Eric
    Eric avatar
    32 posts
    Member since:
    Jun 2011

    Posted 18 May 2012 Link to this post

    This is a really good technique, but unfortuantely only works for the current view. If you switch months, those cells will not be templated.
  9. Eric
    Eric avatar
    32 posts
    Member since:
    Jun 2011

    Posted 21 May 2012 Link to this post

    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.
  10. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 23 May 2012 Link to this post

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