0

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

Katherine Perotta
  • 125
  • 1
  • 5
  • 15

3 Answers3

2

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

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
1

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 );
}
Dai
  • 141,631
  • 28
  • 261
  • 374
0
        **

        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;
                }
            }
kavitha Reddy
  • 3,303
  • 24
  • 14