Calendar Control, DayRender function not executing

4.2k Views Asked by At

I have a calendar in my aspx page that is as follows:

<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>

And the code behind is very simple.

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
   {

    Response.Write("test");
    if (!e.Day.IsToday)
    {
        Label aLabel = new Label();
        aLabel.Text = " <br>test";
        e.Cell.Controls.Add(aLabel);
        date.Text = e.Day.ToString();
    }     
}

My ultimate goal is to add text from a database for events but at the moment I cannot even make the function itself execute. The response.writes are nowhere on the page and the word "test" does not appear on all of the dates except today (which is what should happen to my understanding).

3

There are 3 best solutions below

1
On
        **

        In source View
        --------------

        **

        <asp:Calendar runat="server" ID="cld_date_required" BackColor="White" BorderColor="#999999"CellPadding="4" DayNameFormat="Shortest"
 Font-Names="Arial" Font-Size="8pt" ForeColor="Black" 
 OnDayRender="DayRender" Height="180px" Width="270px"></asp:Calender>


        In Code View
        ------------


     protected void DayRender(object sender, DayRenderEventArgs e)
            {
                if (e.Day.Date < System.DateTime.Today)
                {
                    e.Day.IsSelectable = false;
                    e.Cell.ForeColor = System.Drawing.Color.LightGray;
                }
            }
2
On

The DayRender event requires the event-handler to be mapped. You haven't shown that in your code. You'll need to add this during your Page_Init event (not the Page constructor):

public override void OnInit(Object sender, EventArgs e) {
    this.Calendar1.DayRender += new EventHandler( Calendar1_DayRender );
}
0
On

You can register the render method as follows, saving you writing code for the registration:

<asp:Calendar ID="Calendar1" OnDayRender="Calendar1_DayRender" runat="server">
</asp:Calendar>

NOTE:

"Because the DayRender event is raised while the Calendar control is being rendered, you cannot add a control that can also raise an event, such as LinkButton. You can only add static controls, such as System.Web.UI.LiteralControl, Label, Image, and HyperLink."

UPDATE:

Calendar.OnDayRender Method

Calendar.DayRender Event