自定义日历控件(Calendar)

本文介绍如何创建并使用一个自定义的日历用户控件。该控件采用C#编写,能够实现弹出显示、日期选择等功能,并可以轻松地集成到网页中。

1.先写一个用户控件:

HTML:  

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="RhombusCalendar.ascx.cs" Inherits="Rhombus2.RhombusCalendar" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:Panel id="calendarPanel" Height="176px" Width="192px" runat="server" BackColor="White">
            <asp:Calendar id="Calendar1" runat="server" PrevMonthText="<img src='../../image/monthleft.gif' border=0>"
                        NextMonthText="<img src='../../Image/monthright.gif' border=0>"></asp:Calendar>
</asp:Panel>
<script event="onclick" for="document">
            var count = document.all.length;
            var i;
            for(i=0;i<count;i++)
            {
             if(document.all(i).tagName == "DIV")
             {
                var calendarPanelId = document.all(i).id;
               
                  if(calendarPanelId.lastIndexOf("_calendarPanel") != -1)
                  {
                     document.all(i).style.display="none";
                  }
             }
            }
</script>
.CS
namespace Rhombus2
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 using System.Xml;
 /// <summary>
 ///  Summary description for RhombusCalendar.
 /// </summary>
 public class RhombusCalendar : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.Calendar Calendar1;
  private static string selectedDate = "";
  protected System.Web.UI.WebControls.Panel calendarPanel;
  private static string inputDate;
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
   //if(!IsPostBack)
   {
    calendarPanel.Visible = false;
   }
  }
  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///  Required method for Designer support - do not modify
  ///  the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.Calendar1.VisibleMonthChanged += new System.Web.UI.WebControls.MonthChangedEventHandler(this.Calendar1_VisibleMonthChanged);
   this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
  private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
  {
   string dateFormat = "yyyy-M-dd";
   if(dateFormat==""||dateFormat==null ) dateFormat="yyyy-M-dd";//use default.
   selectedDate = Calendar1.SelectedDate.ToString(dateFormat);
   calendarPanel.Visible = false;
   TextBox txtBox = (TextBox)this.Parent.FindControl(inputDate);
   txtBox.Text = selectedDate;
   
  }
  public void SetTextBox(string txtBoxId)
  {
   inputDate = txtBoxId;
  }
  private void Calendar1_VisibleMonthChanged(object sender, System.Web.UI.WebControls.MonthChangedEventArgs e)
  {
   calendarPanel.Visible = true;
  }
 
  public bool pVisible
  {
   get{return calendarPanel.Visible;}
   set{calendarPanel.Visible = value;}
  }
  public string pLayerOrder
  {
   set{calendarPanel.Style["z-index"] = value;}
  }
  public string pPosition
  {
   set{calendarPanel.Style["position"] = value;}
  }
  public string pTop
  {
   get{return calendarPanel.Style["top"];}
   set{calendarPanel.Style["top"] = value;}
  }
  public string pLeft
  {
   get{return calendarPanel.Style["left"];}
   set{calendarPanel.Style["left"] = value;}
  }
 }
}
2.把用户控件插入需要用的页面里面,最好放入TABLE:
HTML:
 
<%@ Register TagPrefix="uc1" TagName="RhombusCalendar" Src="RhombusCalendar.ascx" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplicationTest.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">
                                    <TABLE id="Table1" style="Z-INDEX: 103; LEFT: 176px; WIDTH: 313px; POSITION: absolute; TOP: 72px; HEIGHT: 88px"
                                                cellSpacing="1" cellPadding="1" width="313" border="1">
                                                <TR>
                                                            <TD>
                                                                        <asp:TextBox id="txt_customerRequestDateFrom" runat="server"></asp:TextBox></TD>
                                                            <td>
                                                                        <asp:Button id="btn_customerRequestDateFrom" runat="server" Text="Button"></asp:Button></td>
                                                </TR>
                                                <TR>
                                                            <TD align="right"></TD>
                                                            <td>
                                                                        <uc1:RhombusCalendar id="CalendarCustReqDateFrom" runat="server"></uc1:RhombusCalendar></td>
                                                </TR>
                                    </TABLE>
                        </form>
            </body>
</HTML>
.CS
 
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 Rhombus2;
 
namespace WebApplicationTest
{
      ///<summary>
      /// Summary description for WebForm1.
      ///</summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.TextBox txt_customerRequestDateFrom;
            protected RhombusCalendar rhombusCalendar;
            protected System.Web.UI.WebControls.Button btn_customerRequestDateFrom;
            private static int i = 400;
     
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }
 
            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
           
            ///<summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            ///</summary>
            private void InitializeComponent()
            {   
                  this.btn_customerRequestDateFrom.Click += new System.EventHandler(this.btn_customerRequestDateFrom_Click);
                  this.Load += new System.EventHandler(this.Page_Load);
 
            }
            #endregion
 
            private void btn_customerRequestDateFrom_Click(object sender, System.EventArgs e)
            {
                  rhombusCalendar = (RhombusCalendar)this.FindControl("CalendarCustReqDateFrom");
                  rhombusCalendar.SetTextBox(txt_customerRequestDateFrom.ID);
                  rhombusCalendar.pPosition = "absolute";
                  rhombusCalendar.pTop = "javascript:getX(btn_customerRequestDateFrom)";
                  rhombusCalendar.pLeft = "javascript:getY(btn_customerRequestDateFrom)";
                  rhombusCalendar.pLayerOrder = "auto";
                  rhombusCalendar.pVisible = true;
                  i++;
                  rhombusCalendar.pLayerOrder = i.ToString();
            }
      }
}
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值