显示日程的自定日历

效果图如下:

示例代码如下:
--------------
HTML部分

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PrivateWork_Default" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>日历</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif        
<table>
None.gif            
<tr>
None.gif                
<td colspan="7" align="center">日历</td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td colspan="7">
None.gif                
<asp:Button ID="btn_PreYear" runat="server" Text="<<" OnClick="btn_PreYear_Click" />
None.gif                
<asp:Button ID="btn_PreMonth" runat="server" Text="<" OnClick="btn_PreMonth_Click" />
None.gif                    
<asp:DropDownList ID="ddl_Year" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_Year_SelectedIndexChanged">
None.gif                    
</asp:DropDownList>
None.gif                    
<asp:DropDownList ID="ddl_Month" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_Month_SelectedIndexChanged">
None.gif                        
<asp:ListItem Text="1月" Value="1"></asp:ListItem>
None.gif                        
<asp:ListItem Text="2月" Value="2"></asp:ListItem>
None.gif                        
<asp:ListItem Text="3月" Value="3"></asp:ListItem>
None.gif                        
<asp:ListItem Text="4月" Value="4"></asp:ListItem>
None.gif                        
<asp:ListItem Text="5月" Value="5"></asp:ListItem>
None.gif                        
<asp:ListItem Text="6月" Value="6"></asp:ListItem>
None.gif                        
<asp:ListItem Text="7月" Value="7"></asp:ListItem>
None.gif                        
<asp:ListItem Text="8月" Value="8"></asp:ListItem>
None.gif                        
<asp:ListItem Text="9月" Value="9"></asp:ListItem>
None.gif                        
<asp:ListItem Text="10月" Value="10"></asp:ListItem>
None.gif                        
<asp:ListItem Text="11月" Value="11"></asp:ListItem>
None.gif                        
<asp:ListItem Text="12月" Value="12"></asp:ListItem>
None.gif                    
</asp:DropDownList>
None.gif                
<asp:Button ID="btn_NextMonth" runat="server" Text=">" OnClick="btn_NextMonth_Click" />
None.gif                
<asp:Button ID="btn_NextYear" runat="server" Text=">>" OnClick="btn_NextYear_Click" />
None.gif                
</td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td></td>
None.gif                
<td></td>
None.gif                
<td></td>
None.gif                
<td></td>
None.gif                
<td></td>
None.gif                
<td></td>
None.gif                
<td></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td runat="server" id="d1"></td>
None.gif                
<td runat="server" id="d2"></td>
None.gif                
<td runat="server" id="d3"></td>
None.gif                
<td runat="server" id="d4"></td>
None.gif                
<td runat="server" id="d5"></td>
None.gif                
<td runat="server" id="d6"></td>
None.gif                
<td runat="server" id="d7"></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td runat="server" id="d8"></td>
None.gif                
<td runat="server" id="d9"></td>
None.gif                
<td runat="server" id="d10"></td>
None.gif                
<td runat="server" id="d11"></td>
None.gif                
<td runat="server" id="d12"></td>
None.gif                
<td runat="server" id="d13"></td>
None.gif                
<td runat="server" id="d14"></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td runat="server" id="d15"></td>
None.gif                
<td runat="server" id="d16"></td>
None.gif                
<td runat="server" id="d17"></td>
None.gif                
<td runat="server" id="d18"></td>
None.gif                
<td runat="server" id="d19"></td>
None.gif                
<td runat="server" id="d20"></td>
None.gif                
<td runat="server" id="d21"></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td runat="server" id="d22"></td>
None.gif                
<td runat="server" id="d23"></td>
None.gif                
<td runat="server" id="d24"></td>
None.gif                
<td runat="server" id="d25"></td>
None.gif                
<td runat="server" id="d26"></td>
None.gif                
<td runat="server" id="d27"></td>
None.gif                
<td runat="server" id="d28"></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td runat="server" id="d29"></td>
None.gif                
<td runat="server" id="d30"></td>
None.gif                
<td runat="server" id="d31"></td>
None.gif                
<td runat="server" id="d32"></td>
None.gif                
<td runat="server" id="d33"></td>
None.gif                
<td runat="server" id="d34"></td>
None.gif                
<td runat="server" id="d35"></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td runat="server" id="d36"></td>
None.gif                
<td runat="server" id="d37"></td>
None.gif                
<td runat="server" id="d38"></td>
None.gif                
<td runat="server" id="d39"></td>
None.gif                
<td runat="server" id="d40"></td>
None.gif                
<td runat="server" id="d41"></td>
None.gif                
<td runat="server" id="d42"></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td bgcolor="#EBF7EA"></td>
None.gif                
<td colspan="6" >
None.gif                 某日有事件
</td>
None.gif              
</tr>
None.gif              
<tr>
None.gif                
<td bgcolor="#FFEEFF"></td>
None.gif                
<td colspan="6" >
None.gif                今天
</td>
None.gif              
</tr>
None.gif              
<tr>
None.gif                
<td bgcolor="#003366"></td>
None.gif                
<td colspan="6" >
None.gif                查看的日期
</td>
None.gif              
</tr>
None.gif        
</table>
None.gif        
<br />
None.gif        
<br />
None.gif        当前所点击的日期
<asp:TextBox ID="txt_NowClickDate" runat="server"></asp:TextBox>
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif

CS部分

ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
None.gif
public partial class PrivateWork_Default : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Page_Load#region Page_Load
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fn_SetYear();
InBlock.gif            
if (!string.IsNullOrEmpty(Request["Year"]))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ddl_Year.SelectedValue = Request["Year"];
InBlock.gif                
this.ddl_Month.SelectedValue = Request["Month"];                
ExpandedSubBlockEnd.gif            }

InBlock.gif            fn_SetCal();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
页面事件#region 页面事件
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
前一年#region 前一年
InBlock.gif    
protected void btn_PreYear_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int iIndex = this.ddl_Year.SelectedIndex;
InBlock.gif        
if ( iIndex!= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ddl_Year.SelectedIndex = iIndex - 1;
InBlock.gif            fn_SetCal();
ExpandedSubBlockEnd.gif        }
        
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
前一月#region 前一月
InBlock.gif    
protected void btn_PreMonth_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int iIndex = this.ddl_Month.SelectedIndex;
InBlock.gif        
if (iIndex != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ddl_Month.SelectedIndex = iIndex - 1;
InBlock.gif            fn_SetCal();
ExpandedSubBlockEnd.gif        }
  
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
后一月#region 后一月
InBlock.gif    
protected void btn_NextMonth_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int iIndex = this.ddl_Month.SelectedIndex;
InBlock.gif        
if (iIndex != 11)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ddl_Month.SelectedIndex = iIndex + 1;
InBlock.gif            fn_SetCal();
ExpandedSubBlockEnd.gif        }
  
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
后一年#region 后一年
InBlock.gif    
protected void btn_NextYear_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int iIndex = this.ddl_Year.SelectedIndex;
InBlock.gif        
if (iIndex != 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ddl_Year.SelectedIndex = iIndex + 1;
InBlock.gif            fn_SetCal();
ExpandedSubBlockEnd.gif        }
      
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
年 DropDownList 选择 改变#region 年 DropDownList 选择 改变
InBlock.gif    
protected void ddl_Year_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        fn_SetCal();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
月 DropDownList 选择 改变#region 月 DropDownList 选择 改变
InBlock.gif    
protected void ddl_Month_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        fn_SetCal();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
自定义方法#region 自定义方法
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
根据日期 设置年份#region 根据日期 设置年份
InBlock.gif    
private void fn_SetYear()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int iYear = DateTime.Today.Year;
InBlock.gif        
//前五年
InBlock.gif
        for (int i = 5; i > 0; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int tmpYear = iYear - i;
InBlock.gif            
this.ddl_Year.Items.Add(new ListItem(tmpYear.ToString()+"",tmpYear.ToString()));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//当前年
InBlock.gif
        this.ddl_Year.Items.Add(new ListItem(iYear.ToString() + "", iYear.ToString()));
InBlock.gif        
//后五年
InBlock.gif
        for (int j = 1; j < 6; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int tmpYear = iYear + j;
InBlock.gif            
this.ddl_Year.Items.Add(new ListItem(tmpYear.ToString() + "", tmpYear.ToString()));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//
InBlock.gif
        this.ddl_Year.SelectedValue = iYear.ToString();
InBlock.gif        
this.ddl_Month.SelectedValue = DateTime.Today.Month.ToString();
InBlock.gif        
this.txt_NowClickDate.Text = DateTime.Today.ToShortDateString();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
根据日期 设置当前的日历#region 根据日期 设置当前的日历
InBlock.gif    
private void fn_SetCal()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int iYear = Convert.ToInt32(this.ddl_Year.SelectedValue);//
InBlock.gif
        int iMonth = Convert.ToInt32(this.ddl_Month.SelectedValue);//月        
InBlock.gif
        DateTime dStart = new DateTime(iYear,iMonth,1);//该年月的第一天
InBlock.gif
        int iDays = DateTime.DaysInMonth(iYear,iMonth);//该年月的总共天数
InBlock.gif
        int iStartDay = (Convert.ToInt32(dStart.DayOfWeek) + 1);//第一天为周几
InBlock.gif        
//当前选中日期
InBlock.gif
        int iSelectDay = 0;
InBlock.gif        
if(string.IsNullOrEmpty(Request["Day"]))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            iSelectDay 
= DateTime.Today.Day;            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            iSelectDay 
= Convert.ToInt32(Request["Day"]);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//日历绘制
InBlock.gif
        for (int i = 1; i < 43; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HtmlTableCell tmpCell 
= (HtmlTableCell)FindControl("d" + i);
InBlock.gif            tmpCell.BgColor 
= "";
InBlock.gif            
if ((i < iStartDay) || ((i - iStartDay + 1> iDays))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tmpCell.InnerText 
= "";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//手形
InBlock.gif
                tmpCell.Style.Add("cursor","hand");
InBlock.gif                
string strOnClickJS = "window.location='Default.aspx?Year=" + iYear.ToString();
InBlock.gif                strOnClickJS 
+= "&Month=" + iMonth.ToString();
InBlock.gif                strOnClickJS 
+= "&Day=" + Convert.ToString(i - iStartDay + 1+ "";
InBlock.gif                tmpCell.Attributes.Add(
"onclick", strOnClickJS);//页面回发 以便到更多服务器端自定义处理
InBlock.gif                
//得到当前绘制的日期
InBlock.gif
                DateTime currDrawDate = new DateTime(iYear, iMonth, (i - iStartDay + 1));
InBlock.gif                
//得到当前绘制的日期的事件数
InBlock.gif
                int iCount = fn_GetEventCount(currDrawDate);
InBlock.gif                
if (DateTime.Equals(currDrawDate, DateTime.Today))//等于今天
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    tmpCell.InnerText 
= Convert.ToString((i - iStartDay + 1));
InBlock.gif                    tmpCell.BgColor 
= "#FFEEFF";
InBlock.gif                    
if (iCount != 0)//有日志
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        tmpCell.Attributes.Add(
"title""" + iCount.ToString() + "个事件");                       
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else//不是今天
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    tmpCell.InnerText 
= Convert.ToString((i - iStartDay + 1));
InBlock.gif                    
if (iCount != 0)//有日志
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        tmpCell.Attributes.Add(
"title""" + iCount.ToString() + "个事件");
InBlock.gif                        tmpCell.BgColor 
= "#EBF7EA";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
//当前查看日期
InBlock.gif
            if ((i - iStartDay + 1== iSelectDay)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tmpCell.BgColor 
= "#003366";
InBlock.gif                
this.txt_NowClickDate.Text = this.ddl_Year.SelectedValue + "-" + this.ddl_Month.SelectedValue + "-" + iSelectDay.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
得到当天事件日志的笔数#region 得到当天事件日志的笔数
InBlock.gif    
private int fn_GetEventCount(DateTime dDate)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int iCount = 0;
InBlock.gif        
string strSql = " SELECT COUNT(EventSeqID) FROM CalEvent ";
InBlock.gif        strSql 
+= " WHERE EventStartDate<='"+dDate.ToShortDateString()+"";
InBlock.gif        strSql 
+= " AND EventEndDate>='" + dDate.ToShortDateString() + "";
InBlock.gif        DBClass db 
= new DBClass();
InBlock.gif        iCount 
= Convert.ToInt32(db.RunSelectGetScalar(strSql));
InBlock.gif        
return iCount;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}

None.gif


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值