前台页面
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Blog_calendar.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:calendar id="Calendar1" CellPadding="2" Width="160px" TitleStyle-BackColor="#000000" BorderColor="#aaaaaa"
    DayHeaderStyle-BackColor="#5e715e" OtherMonthDayStyle-ForeColor="#cccccc" DayNameFormat="Full"
    runat="server" TitleStyle-ForeColor="#ffffff" NextPrevStyle-ForeColor="#ffffff" CellSpacing="1"
    WeekendDayStyle-BackColor="#eeeeee" DayHeaderStyle-ForeColor="#ffffff" SelectionMode="None"
    TodayDayStyle-BorderColor="#5e715e" TodayDayStyle-BorderWidth="1" TodayDayStyle-Font-Bold="true"
    TodayDayStyle-ForeColor="#5e715e">
    <TodayDayStyle Font-Bold="True" BorderWidth="1px" ForeColor="#5E715E" BorderColor="#5E715E"></TodayDayStyle>
    <NextPrevStyle ForeColor="White"></NextPrevStyle>
    <DayHeaderStyle ForeColor="White" BackColor="#404040"></DayHeaderStyle>
    <TitleStyle ForeColor="White" BackColor="Black"></TitleStyle>
    <WeekendDayStyle BackColor="Silver"></WeekendDayStyle>
    <OtherMonthDayStyle ForeColor="#CCCCCC"></OtherMonthDayStyle>
   </asp:calendar>
  </form>
 </body>
</HTML>

后台程序:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;

namespace Blog_calendar
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Calendar Calendar1;
  private int[] arrCurrentDays,arrPreDays,arrNextDays; //三个变量分别是当前月,前一月,和下一个月
  private int intCurrentMonth,intPreMonth,intNextMonth; //三个整型数组存放相对月份写有blog的日期
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  private void Calendar1_PreRender(object sender, System.EventArgs e)
  {
   Thread threadCurrent = Thread.CurrentThread;
   CultureInfo ciNew = (CultureInfo)threadCurrent.CurrentCulture.Clone();
   ciNew.DateTimeFormat.DayNames = new string[]{"日","一","二","三","四","五","六"};
   ciNew.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
   threadCurrent.CurrentCulture = ciNew;
  }

  private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
  {
   //该控件在创建每一天时发生。  
   CalendarDay d = ((DayRenderEventArgs)e).Day;
   TableCell c   = ((DayRenderEventArgs)e).Cell;  
   // 初始化当前月有Blog的日期数组
   if( intPreMonth == 0 )
   {
    intPreMonth = d.Date.Month; // 注意:日历控件初始化时我们得到的第一个月并不是当前月,而是前一个月的月份
    //Response.Write(d.Date.Month.ToString());
    intCurrentMonth = intPreMonth+1;
       if (intCurrentMonth>12)
     intCurrentMonth=1 ;
    intNextMonth = intCurrentMonth+1;
    if (intNextMonth >12)
     intNextMonth =1;
    arrPreDays =    getArrayDay(d.Date.Year,intPreMonth); //得到前一个月有blog的日期数组
    arrCurrentDays = getArrayDay(d.Date.Year,intCurrentMonth) ;//得到当月有blog的日期数组
    arrNextDays =   getArrayDay(d.Date.Year,intNextMonth) ;//得到下个月有blog的日期数组
   }
  
   int j=0;   
   

   if( d.Date.Month.Equals(intPreMonth) )
   {
    while( ! arrPreDays[j].Equals(0) )
    {
     if(d.Date.Day.Equals(arrPreDays[j]))
     {
      c.Controls.Clear();
      c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?year="+d.Date.Year+"-"+
       d.Date.Month+"-"+d.Date.Day+">"+d.Date.Day+"</a>"));
     }
     j++;
    }
   }
   else if( d.Date.Month.Equals(intCurrentMonth) )
   {
    while( !arrCurrentDays[j].Equals(0) )
    {
     if(d.Date.Day.Equals(arrCurrentDays[j]))
     {
      c.Controls.Clear();
      c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?time="+d.Date.Year+"-"+
       d.Date.Month+"-"+d.Date.Day+" title='"+d.Date.Year+'/'+d.Date.Month+'/'+d.Date.Day+"'>"+d.Date.Day+"</a>"));
     }
     j++;
    }
   }
   else if( d.Date.Month.Equals(intNextMonth) )
   {
    while( ! arrNextDays[j].Equals(0) )
    {
     if(d.Date.Day.Equals(arrNextDays[j]))
     {
      c.Controls.Clear();
      c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?year="+d.Date.Year+"-"+
       d.Date.Month+"-"+d.Date.Day+">"+d.Date.Day+"</a>"));
     }
     j++;
    }
   }
  }

  private int[] getArrayDay(int intYear,int intMonth)
  {
   int i=0;
   int[] intArray = new int[31];
   //从数据库里选取符合要求的记录,将日期存入数组
   string mySelectQuery = "select OrderDate from test where year(OrderDate)="+intYear+
    " and month(OrderDate)="+intMonth;
   SqlConnection myConnection=new SqlConnection("server=localhost;UID=sa;pwd=fan;database=test");
   SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
   myConnection.Open();
   SqlDataReader myReader;
   myReader = myCommand.ExecuteReader();
   while (myReader.Read())
   {
    if( i==0 )
    {
     intArray[i] = myReader.GetDateTime(0).Day;
     i++;
    }
    else if( myReader.GetDateTime(0).Day != intArray[i-1] )
    {
     intArray[i] = myReader.GetDateTime(0).Day;
     i++;
    }
   }
   myReader.Close();
   myConnection.Close();
   
   return intArray;
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
   this.Calendar1.PreRender += new System.EventHandler(this.Calendar1_PreRender);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

posted on 2004-10-14 18:17  fghr  阅读(709)  评论(0编辑  收藏  举报