Your calendar (C#) : Calendar « Components « ASP.Net






Your calendar (C#)

<%--
Beginning ASP.NET 1.0 with C# (Paperback)
by David Sussman, Chris Ullman, 
   Juan T. Llibre, John Kauffman, 
   Ollie Cornes, Ajoy Krishnamoorthy, 
   Srinivasa Sivakumar, Chris Goode, 
   Neil Raybould, Christopher Miller, 
   Rob Birdwell, Matt Butler, Gary Johnson 
   
# Publisher: Wrox Press; 1st edition (June 2002)
# Language: English
# ISBN: 1861007345
--%>


<%@ Page Language="c#" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Globalization" %>

<html>
<head>
<script language="c#" runat="server">

protected void Page_Load(object sender, EventArgs e)
  {
    if (!(IsPostBack))
    {
       ShowDailyEvents();
    }
  }

  public void MyCalendar_SelectionChanged(object sender, EventArgs e)
  {
    ShowDailyEvents();
  }

  protected DataSet LoadMyCalendarData()
  {
    string sourceXml = Server.MapPath("MyCalendar.xml");
    if (!(File.Exists(sourceXml)))
    {
       return null;
    }
    DataSet cachedDataSet = (DataSet)Session["MyCalendarData"];
    if (cachedDataSet != null)
    { 
       return cachedDataSet;
    }
    DataSet dataSet = new DataSet();
    try 
    {
      dataSet.ReadXml(sourceXml);
      Session["MyCalendarData"] = dataSet;
    }
    catch (Exception e)
    {
      SelectedDate.Text = e.Message;
      dataSet = null;
    }
    return dataSet;
  }

  protected void MyCalendar_DayRender(object sender, DayRenderEventArgs e)
  {
    if (e.Day.IsOtherMonth)
    {
       e.Cell.BackColor=System.Drawing.Color.FromName("Gainsboro");
    }
    else
    {
      if (e.Day.IsWeekend)
      {
        e.Cell.BackColor=System.Drawing.Color.FromName("PaleGoldenrod");
      }
      else
      {
         e.Cell.BackColor=System.Drawing.Color.FromName("LightGoldenrodYellow");
      }
    }
    DataSet dataSet = LoadMyCalendarData();
    if (dataSet == null)
    {
      return;
    }
    foreach (DataRow zRow in dataSet.Tables[0].Rows)
    {
      DateTime compareDate = GetSafeDate(zRow["EventDate"].ToString());
      if (compareDate == e.Day.Date)
      {
        // Event matches date criteria ?display it...
        MyCalendarEventData myEventData = new MyCalendarEventData();
        myEventData.ShortDesc = zRow["ShortDesc"].ToString();
        myEventData.DetailDesc = zRow["DetailDesc"].ToString();
        myEventData.StartTime = zRow["StartTime"].ToString();
        myEventData.EndTime = zRow["EndTime"].ToString();
        Label dailyEventLabel = new Label();
        dailyEventLabel.Text = "<br />" + myEventData.ShortDesc;
        e.Cell.Controls.Add(dailyEventLabel);
      }
    }
  }

  protected void ShowDailyEvents()
  {
    DateTime d = MyCalendar.SelectedDate;
    DataSet dataSet = LoadMyCalendarData();
    if (dataSet == null)
    {
      return;
    }
    ArrayList aEvents = new ArrayList();
    foreach (DataRow zRow in dataSet.Tables[0].Rows)
    {
      DateTime compareDate = GetSafeDate(zRow["EventDate"].ToString());
      if (compareDate == d) 
      {
        // Event matches date criteria ?display it...
        MyCalendarEventData myEventData = new MyCalendarEventData();
        myEventData.EventDate = d;
        myEventData.ShortDesc = zRow["ShortDesc"].ToString();
        myEventData.DetailDesc = zRow["DetailDesc"].ToString();
        myEventData.StartTime = zRow["StartTime"].ToString();
        myEventData.EndTime = zRow["EndTime"].ToString();
        aEvents.Add(myEventData);
      }
    }
    // Bind to the Repeater control...
    DailyEventDetailRepeater.DataSource = aEvents;
    DailyEventDetailRepeater.DataBind();
    if (aEvents.Count > 0)
    {
      DailyDetailsPanel.Visible = true;
      SelectedDate.Text = "Events For " + d.ToLongDateString();
    }
    else
    {
      DailyDetailsPanel.Visible = false;
      SelectedDate.Text = "No Events Scheduled For " + d.ToLongDateString();
    }
  }

  private DateTime GetSafeDate(string proposedDate)
  {
    // Returns a non-null DateTime even if proposed date can't be parsed
    DateTime safeDate;
    try {
      safeDate = DateTime.Parse(proposedDate, DateTimeFormatInfo.InvariantInfo);
    } catch (Exception e) {
      Response.Write("<!-- Failed to parse date: " + e.Message + " -->");
      safeDate = DateTime.MinValue; 
    }
    return safeDate;
  }

  public class MyCalendarEventData
  {
    private string m_ShortDesc;
    private string m_DetailDesc;
    private DateTime m_EventDate;
    private string m_StartTime;
    private string m_EndTime;
    public string ShortDesc
    {
      get {
        return m_ShortDesc;
      }
      set {
       m_ShortDesc = value;
      }
    }
    public string DetailDesc
    {
      get {
        return m_DetailDesc;
      }
      set {
        m_DetailDesc = value;
      }
    }
    public DateTime EventDate
    {
      get {
        return m_EventDate;
      }
      set {
        m_EventDate = value;
      }
    }
    public string StartTime
    {
      get {
        return m_StartTime;
      }
      set {
        m_StartTime = value;
      }
    }
    public string EndTime
    {
      get {
        return m_EndTime;
      }
      set {
        m_EndTime = value;
      }
    }
  }

</script>

</head>
<body>
<h1>My Calendar</h1>
  <form id="MyCalendarForm" method="post" runat="server">
    <p align="center">
      <asp:Calendar id="MyCalendar" runat="server"
        SelectedDate="2002/07/17"
        VisibleDate="2002/07/01"
        FirstDayOfWeek="Monday"
        DayNameFormat="Full"
        ShowDayHeader="True"
        ShowGridLines="True"
        ShowNextPrevMonth="True"
        ShowTitle="True"
        nextprevstyle-backcolor="DodgerBlue"
        nextprevstyle-forecolor="White"
        nextprevstyle-font-bold="True"
        nextprevstyle-font-size="Large"
        TitleFormat="MonthYear"
        TitleStyle-BackColor="DodgerBlue"
        TitleStyle-ForeColor="White"
        TitleStyle-Font-Size="Large"
        TitleStyle-Font-Bold="True"
        dayheaderstyle-backcolor="DodgerBlue"
        dayheaderstyle-forecolor="White"
        daystyle-horizontalalign="Left"
        daystyle-verticalalign="Top"
        daystyle-font-size="Small"
        SelectedDayStyle-Font-Bold="True"
        selecteddaystyle-horizontalalign="Left"
        selecteddaystyle-verticalalign="Top"
        selecteddaystyle-font-size="Small"
        selecteddaystyle-forecolor="Red"
        TodayDayStyle-HorizontalAlign="Left"
        TodayDayStyle-VerticalAlign="Top"
        todaydaystyle-backcolor="White"

        OnDayRender="MyCalendar_DayRender"
        OnSelectionChanged="MyCalendar_SelectionChanged">

      </asp:Calendar>
    </p>

    <p align="center">
      <asp:label id="SelectedDate" runat="server" font-size="Large" />
    </p>

      <asp:panel id="DailyDetailsPanel" runat="server">
        <asp:Repeater id="DailyEventDetailRepeater" runat="server">
          <HeaderTemplate>
          <p align="center">
          <table border="1" width="100%">
          <table style="color:Black;border collapse:collapse;">
            <tr style="color:White;background-color:DodgerBlue;
                       font-weight:bold;">
              <td><b>Event</b></td>
              <td><b>Description</b></td>
              <td><b>Start Time</b></td>
              <td><b>End Time</b></td>
            </tr>
          </HeaderTemplate>
          <ItemTemplate>
            <tr style="background-color:White;">
              <td><%# DataBinder.Eval(Container.DataItem, "ShortDesc") %></td>
              <td><%# DataBinder.Eval(Container.DataItem, "DetailDesc") %></td>
              <td><%# DataBinder.Eval(Container.DataItem, "StartTime") %></td>
              <td><%# DataBinder.Eval(Container.DataItem, "EndTime") %></td>
            </tr>
          </ItemTemplate >
          <AlternatingItemTemplate>
            <tr style="background-color:Gainsboro;">
              <td><%# DataBinder.Eval(Container.DataItem, "ShortDesc") %></td>
              <td><%# DataBinder.Eval(Container.DataItem, "DetailDesc") %></td>
              <td><%# DataBinder.Eval(Container.DataItem, "StartTime") %></td>
              <td><%# DataBinder.Eval(Container.DataItem, "EndTime") %></td>
            </tr>
          </AlternatingItemTemplate>
          <FooterTemplate>
          </table>
          </p>
          </FooterTemplate>
        </asp:Repeater>
      </asp:panel>

  </form>
</body>
</html>


<%--
<?xml version="1.0" standalone="yes"?>
<MyCalendar>
  <Event>
    <ShortDesc>Gig in Portland - Jazz Club</ShortDesc>
    <DetailDesc>This should be fun - playing J &amp; T again - be sure to bring the charts.</DetailDesc>
    <EventDate>2002/07/02</EventDate>
    <StartTime>6:00PM</StartTime>
    <EndTime>11:30PM</EndTime>
  </Event>
  <Event>
    <ShortDesc> Rehearsal - Brigadoon</ShortDesc>
    <DetailDesc>Community Theatre orchestra rehearsal - bring mutes.</DetailDesc>
    <EventDate>2002/07/14</EventDate>
    <StartTime>3:30PM</StartTime>
    <EndTime>6:30PM</EndTime>
  </Event>
  <Event>
    <ShortDesc>.NET Training Class</ShortDesc>
    <DetailDesc>This should be fun - we'll explore some of the really cool stuff, like ASP.NET server controls and Web Services.</DetailDesc>
    <EventDate>2002/07/17</EventDate>
    <StartTime>8:00AM</StartTime>
    <EndTime>4:30PM</EndTime>
  </Event>
  <Event>
    <ShortDesc>Writing Workshop for Musical Project with Gregg</ShortDesc>
    <DetailDesc>We're going to brainstorm some ideas and see if we can come up with something great.  We're off to a good start.</DetailDesc>
    <EventDate>2002/07/19</EventDate>
    <StartTime>10:00AM</StartTime>
    <EndTime>6:30PM</EndTime>
  </Event>
  <Event>
    <ShortDesc>Community Band</ShortDesc>
    <DetailDesc>Central park - we'll play everything from standards to shows tunes to classical to marches - you name it.  People bring their lawn chairs, eat their dinner, kids play - a great time!</DetailDesc>
    <EventDate>2002/07/24</EventDate>
    <StartTime>7:00PM</StartTime>
    <EndTime>9:00PM</EndTime>
  </Event>
  <Event>
    <ShortDesc>Jam Session at the Beach</ShortDesc>
    <DetailDesc>Bring more food this time and the crab nets - and the instruments!  We'll might stay for a week or so depending on the weather.</DetailDesc>
    <EventDate>2002/07/21</EventDate>
    <StartTime>8:00AM</StartTime>
    <EndTime>11:30PM</EndTime>
  </Event>
  <Event>
    <ShortDesc>Rob's Birthday!</ShortDesc>
    <DetailDesc>Nothing too fancy - just friends and family.  Hope it's a nice day - bike ride would be fun.</DetailDesc>
    <EventDate>2002/07/30</EventDate>
    <StartTime>6:09PM</StartTime>
    <EndTime>11:30PM</EndTime>
  </Event>
  <Event>
    <ShortDesc />
    <DetailDesc />
    <EventDate />
    <StartTime />
    <EndTime />
  </Event>
  <Event>
    <ShortDesc>Event</ShortDesc>
    <DetailDesc>Number </DetailDesc>
    <EventDate>Wrox</EventDate>
    <StartTime>6.00</StartTime>
    <EndTime>11.30</EndTime>
  </Event>
</MyCalendar>

--%>


           
       








Related examples in the same category