Search code examples
c#asp.netusing

My little test program that I got out of the Official MCTS EXAM 70-562 book is not firing the events


My little test program that I got out of the Official MCTS EXAM 70-562 book is not firing the events. I followed through the computer logic in debug mode and it didn't even go into my events even though I have everything set up to handle them. So the big question is what am I doing wrong?

    **CODE BEHIND FILE:**

    public partial class Calendar : System.Web.UI.Page
    {
      Hashtable _scheduleData;

      protected void Page_Load(object sender, EventArgs e)
      {
        _scheduleData = GetSchedule();

        Calendar1.Caption = "Personal Schedule";
        Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
        Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth;
        Calendar1.TitleFormat = TitleFormat.MonthYear;
        Calendar1.ShowGridLines = true;
        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left;
        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top;
        Calendar1.DayStyle.Height = new Unit(75);
        Calendar1.DayStyle.Width = new Unit(100);
        Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.Cornsilk;
        Calendar1.TodaysDate = new DateTime(2009, 2, 1);
        Calendar1.VisibleDate = Calendar1.TodaysDate;
     }

     private Hashtable GetSchedule()
     {
       Hashtable schedule = new Hashtable();

       schedule["2/9/2009"] = "Vactican Day";
       schedule["2/18/2009"]="Budget planning meeting @ 3:00pm";
       schedule["2/24/2009"]="Dinner plans with friends @ 7:00pm";
       schedule["2/27/2009"]="Travel Day";
       schedule["3/5/2009"]="Conf call @ 1:00pm";
       schedule["3/10/2009"]="Meet with art director for lunch";
       schedule["3/27/2009"]="Vacation Day";

       return schedule;
   }

   protected void Calendar1_SelectionChanged(object sender, EventArgs e)
   {
       LabelAction.Text = "Selection changed to: " +       Calendar1.SelectedDate.ToShortDateString();
   }

   protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
   {
       LabelAction.Text = "Month changed to : " + e.NewDate.ToShortDateString();
   }

   protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
   {
     if (_scheduleData[e.Day.Date.ToShortDateString()] != null)
     {
         //Literal lit = new Literal();
         lit.Text = "<br />";
         e.Cell.Controls.Add(lit);

         //Label lbl = new Label ();
         lbl.Text = (string)_scheduleData[e.Day.Date.ToShortDateString()];
         lbl.Font.Size=new FontUnit (FontSize.Small);
         e.Cell.Controls.Add(lbl);
     }
   }   
 }


    **ASPX CODE:**

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calendar.aspx.cs" Inherits="Calendar" %>

    <!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>Untitled Page</title>
    </head>
    <body>
       <form id="form1" runat="server">    
          <div style="height: 350px">   
             <br />
             <asp:Label ID="LabelAction" runat="server" Text=" "></asp:Label> 
             <br />
             <asp:Literal ID="lit" runat="server"></asp:Literal> 
             <br />
             <asp:Label ID="lbl" runat="server" Text=" "></asp:Label> 
             <br />        
             <asp:Calendar ID="Calendar1" runat="server"
                OnSelectionChanged="Calendar1_SelectionChanged"
                OnVisibleMonthChanged="Calendar1_VisibleMonthChanged"
                OnDayRender="Calendar1_DayRender" SelectionMode="DayWeekMonth">  </asp:Calendar>
             <br />        
             <asp:Label ID="Label1" runat="server" Text=" "></asp:Label>      
             <br />      
          </div>
       </form>
    </body>
    </html>

Solution

  • AutoEvent wireup is not happening because you have not linked your methods of .cs file to the control on the .aspx page

    Change your code to fillowing in aspx page source:

           <asp:Calendar  ID="Calendar1" runat="server" 
            ondayrender="Calendar1_DayRender" 
            onselectionchanged="Calendar1_SelectionChanged" 
            onvisiblemonthchanged="Calendar1_VisibleMonthChanged"></asp:Calendar>