刚学ASP。NET,遇到一个例子的问题,没有出错提示,但是没出现预想的程序效果。
程序的功能是实现显示一个日历的事件。
具体为:
1、第一次打开,本月日期背景和字体颜色区别于上一月和下一月,每个日期数字下显示对应的事件短标题。
2、每点击一个日期,最下方就显示这个事件的详细信息。
现在只显示一个日期控件,后台代码写了很多却等于什么都没写。
代码如下:
数据源文件MyCalendar.xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<MyCalendar>
<Event>
<ShortDesc>事件1</ShortDesc>
<DetailDesc>事件1的详细描述,可以有很多的内容.</DetailDesc>
<EventDate>2006-10-1</EventDate>
<StartTime>6:00 PM</StartTime>
<EndTime>11:00 PM</EndTime>
</Event>
<Event>
<ShortDesc>事件2</ShortDesc>
<DetailDesc>事件2的详细描述,可以有很多的内容,也可以有自己的描述.</DetailDesc>
<EventDate>2006-10-7</EventDate>
<StartTime>3:30 PM</StartTime>
<EndTime>6:30 PM</EndTime>
</Event>
<Event>
<ShortDesc>事件3</ShortDesc>
<DetailDesc>事件3的详细描述,可以有很多的内容,也可以有自己的描述和内容.</DetailDesc>
<EventDate>2006-10-15</EventDate>
<StartTime>8:30 PM</StartTime>
<EndTime>14:10 PM</EndTime>
</Event>
</MyCalendar>
界面文件MyCalendar.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ControlCalendar.aspx.cs" Inherits="ControlCalendar" %>
<!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">
<!--日期控件-->
<asp:Calendar ID="MyCalendar" runat="server" SelectedDate="2006-10-1" VisibleDate="2006-10-1"
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="Larger"
TitleFormat="MonthYear" TitleStyle-BackColor="dodgerblue" TitleStyle-ForeColor="white"
Font-Size="12px" ForeColor="Blue" TitleStyle-Font-Size="Larger"
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>
<!--信息显示控件-->
<asp:Label ID="SelectedDate" runat="server" Font-Size="large"></asp:Label>
<!--事件详细信息显示-->
<asp:Panel ID="DDPanel" runat="server">
<asp:Repeater ID="ddReapter" runat="server">
<HeaderTemplate>
<p>
<table style="border-collapse: collapse;">
<tr style="color: White; background-color: DodgerBlu; 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>
对应后台编码:MyCalendar.aspx.cs
using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
public partial class ControlCalendar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!(IsPostBack))
{
ShowDailyEvent();
}
}
public void MyCalendar_SelectionChanged(object sender, EventArgs e)//每单击一个日期在最下面显示详细事件信息
{
ShowDailyEvent();
}
protected DataSet LoadMyCalendarData()//被调用来加载xml文件数据到DataSet
{
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("grainsboro");
}
else
{
if (e.Day.IsWeekend)
{
e.Cell.BackColor = System.Drawing.Color.FromName("PaleGoldnrod");
}
else
{
e.Cell.BackColor = System.Drawing.Color.FromName("lightgoldnrodyellow");
}
}
DataSet dataSet = LoadMyCalendarData();
if (dataSet == null)
{
return;
}
foreach (DataRow zRow in dataSet.Tables[0].Rows)
{
DateTime compareDate = GetSafeDate(zRow["shortdesc"].ToString());
if (compareDate == e.Day.Date)
{
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 ShowDailyEvent()//在最下方显示详细的事件信息
{
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["shortdesc"].ToString());
if (compareDate == d)
{
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);
}
}
ddReapter.DataSource = aEvents;
ddReapter.DataBind();
if (aEvents.Count > 0)
{
DDPanel.Visible = true;
SelectedDate.Text = "Events For" + d.ToLongDateString();
}
else
{
DDPanel.Visible = false;
SelectedDate.Text = "NO Events scheduled for" + d.ToLongDateString();
}
}
private DateTime GetSafeDate(string proposedDate)//格式化一个日期
{
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 shortDesc;
private string detailDesc;
private DateTime eventDate;
private string startTime;
private string endTime;
public string ShortDesc
{
get
{
return shortDesc;
}
set
{
shortDesc = value;
}
}
public string DetailDesc
{
get
{
return detailDesc;
}
set
{
detailDesc = value;
}
}
public DateTime EventDate
{
get
{
return eventDate;
}
set
{
eventDate = value;
}
}
public string StartTime
{
get
{
return startTime;
}
set
{
startTime = value;
}
}
public string EndTime
{
get
{
return endTime;
}
set
{
endTime = value;
}
}
}
}
[此贴子已经被作者于2006-10-15 20:45:53编辑过]