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
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.
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.
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 |
} |
} |
} |
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.
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
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