一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ASP.NET中实现弹出日历实例程序

时间:2013-07-23 编辑:简简单单 来源:一聚教程网

全.net实现弹出日历

 代码如下 复制代码

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="ctlCalendar.ascx.cs" Inherits="calendar.ctlCalendar" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" enableViewState="True"%>




    DayNameFormat="Full" ForeColor="Black" Font-Size="8pt" Font-Names="Verdana" BorderColor="#999999"
  CellPadding="4" Width="200px" Height="180px">
 
 
 
 
 
 
 
 
 
 

cs代码

 代码如下 复制代码


namespace calendar
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 ///


 ///  ctlCalendar 的摘要说明。
 ///

 public class ctlCalendar : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Panel pnlCalendar;
  protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
  protected System.Web.UI.WebControls.Calendar Calendar1;
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if (!Page.IsPostBack)
   {
    this.TextBox1.Text = System.DateTime.Now.ToShortDateString();
    this.pnlCalendar.Attributes.Add("style","DISPLAY: none; POSITION: absolute");
   }
   else
   {
    string id = Page.Request.Form["__EVENTTARGET"].Substring(0,Page.Request.Form["__EVENTTARGET"].IndexOf(":"));
    if (id != this.ID)
    {
     this.pnlCalendar.Attributes.Add("style","DISPLAY: none; POSITION: absolute");
    }
    else
    {
     this.pnlCalendar.Attributes.Add("style","POSITION: absolute");
    }
   }
   Page.RegisterClientScriptBlock("Script_Panel" + this.ID,
    "");  
   this.Button1.Attributes.Add("OnClick","On"+this.ID+"Click()");
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  ///
  ///  设计器支持所需的方法 - 不要使用代码编辑器
  ///  修改此方法的内容。
  ///

  private void InitializeComponent()
  {
   this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
  #region 日历选择时的事件
  private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
  {
   this.TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
   this.pnlCalendar.Attributes.Add("style","DISPLAY: none; POSITION: absolute");
  }
  #endregion
 }
}

好了下面结果js+.net实现弹出日历

在需要调用日期选择的页面放置两个TEXTBOX与BUTTON以选择开始时间与结束时间,并在html代码的 之前加入如下javascript语句:

 

 代码如下 复制代码

 

以上语句定义了两个模态对话框,当调用模态对话框时打开CalendarForm2.aspx页面选择日期,本页面窗体FORM名称为Form1,两个TextBox分别接收传递进来的两个时间值而且应该能互不影响。注意html中窗体的定义应该与javascript中定义的对应并且应该是服务器端运行的,如

在本页面WebForm1.aspx.cs代码部分页面加载Page_Load事件内加入如下语句将定义的javascript行为赋予Button:

 

 代码如下 复制代码
  ButtonBeginDate.Attributes.Add("onclick", "openModeBegin()");
   ButtonEndDate.Attributes.Add("onclick", "openModeEnd()");

CalendarForm2.aspx是个临时容器,提供框架调用CalendarForm3.aspx的内容,以备关掉日期选择窗体后无法完成传值,在其html的Head标记之间应该加入如下语句:

 代码如下 复制代码

CalendarForm2.aspx.cs文件中亦不需要写任何代码,只需在body标记之间加入如下代码: 
 

 代码如下 复制代码

 
   <iframe frameborder="no" src='CalendarForm3.aspx' style="WIDTH: 480px; HEIGHT: 450px" id="IFRAME1"
    language="javascript" onblur="return IFRAME1_onblur()">
 

 

CalendarForm3.aspx我们实际用到的日期选择页面包含一个日历控件与一个Button一个TextBox,此处直接将日历控件Calendar的选定值传给第一个页面WebForm1.aspx更简单,但我们没有这样做,不直接传值主要是考虑到大多数用户的使用习惯,在此将日历控件选中的值传给页面上的TextBox,按下Button后再传给WebForm1.aspx,还可以在用户误选后容易纠正。

热门栏目