ASP.NET 程式實現部落格日曆

來源:互聯網
上載者:User
asp.net|部落格|程式|日曆

  效果圖:

  前台頁面

<%@ 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 Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   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
 }
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.