ASP.NET中实现弹出日历示例

所属分类: 网络编程 / ASP.NET 阅读数: 494
收藏 0 赞 0 分享

在.net中弹出日历的方法有很多种,这里介绍直接使用.net来实例,我们当然还可以使用js日历来实例哦,下面我分别简单举两个实例吧。有需要的朋友可以了解一下。

代码如下:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="ctlCalendar.ascx.cs" Inherits="calendar.ctlCalendar" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" enableViewState="True"%>
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<input type="button" id="Button1" runat="server" value="..."><br>
<asp:Panel id="pnlCalendar" runat="server" style="POSITION: absolute">
 <asp:calendar id="Calendar1" runat="server" FirstDayOfWeek="Monday" ShowGridLines="True" BackColor="White"
 DayNameFormat="Full" ForeColor="Black" Font-Size="8pt" Font-Names="Verdana" BorderColor="#999999"
 CellPadding="4" Width="200px" Height="180px">
 <TodayDayStyle ForeColor="Black" BackColor="#CCCCCC"></TodayDayStyle>
 <SelectorStyle BackColor="#CCCCCC"></SelectorStyle>
 <DayStyle Wrap="False" BorderStyle="Dashed"></DayStyle>
 <NextPrevStyle VerticalAlign="Bottom"></NextPrevStyle>
 <DayHeaderStyle Font-Size="X-Small" Font-Names="宋体" Wrap="False" BorderStyle="Dashed" BackColor="#CCCCCC"></DayHeaderStyle>
 <SelectedDayStyle Font-Bold="True" ForeColor="White" BackColor="#666666"></SelectedDayStyle>
 <TitleStyle Font-Size="Small" Font-Bold="True" BorderStyle="Solid" BorderColor="Black" BackColor="#999999"></TitleStyle>
 <WeekendDayStyle BackColor="LightSteelBlue"></WeekendDayStyle>
 <OtherMonthDayStyle ForeColor="Gray"></OtherMonthDayStyle>
 </asp:calendar>
</asp:Panel>

cs代码

namespace calendar
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 /// <summary>
 /// ctlCalendar 的摘要说明。
 /// </summary>
 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,
  "<script> function On"+this.ID+"Click() { if("+this.ID+
"_pnlCalendar.style.display == "none")   "+this.ID+
"_pnlCalendar.style.display = "";  else  "+this.ID+
"_pnlCalendar.style.display = "none"; } </script>");  
  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);
 }
 /// <summary>
 /// 设计器支持所需的方法 - 不要使用代码编辑器
 /// 修改此方法的内容。
 /// </summary>
 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代码的 </body>之前加入如下

javascript语句:

<script language="javascript"> 
   function openModeBegin() 
   { 
   var returnValue=window.showModalDialog("CalendarForm2.aspx",Form1.TextBoxBeginDate.value);
   Form1.TextBoxBeginDate.value=returnValue; 
   } 
 </script> 
 <script language="javascript"> 
   function openModeEnd() 
   { 
   var returnValue=window.showModalDialog("CalendarForm2.aspx",Form1.TextBoxEndDate.value); 
   Form1.TextBoxEndDate.value=returnValue; 
   } 
 </script>

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

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

ButtonBeginDate.Attributes.Add("onclick", "openModeBegin()"); 
  ButtonEndDate.Attributes.Add("onclick", "openModeEnd()");

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

代码如下:

<script id="clientEventHandlersJS" language="javascript"> 
<!-- 
function IFRAME1_onblur() {} 
//--> 
 </script>

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

代码如下:

<body runat="server" ID="Body1"> 
 <form id="Form1" method="post" runat="server"> 
  <iframe frameborder="no" src='CalendarForm3.aspx' style="WIDTH: 480px; HEIGHT: 450px" id="IFRAME1" 
  language="javascript" onblur="return IFRAME1_onblur()"></iframe> 
 </form> 
</body>

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

asp.net 页面间传值与跳转的区别

通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
收藏 0 赞 0 分享

ASP.NET Gridview与checkbox全选、全不选实现代码

ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传。
收藏 0 赞 0 分享

ASP.NET DropDownList控件的使用方法

ASP.NET DropDownList控件的使用方法,学习asp.net的朋友没用过这个控件的朋友可以参考下。
收藏 0 赞 0 分享

一些.NET对多线程异常处理技巧分享

多线程应用,在实际的项目或产品开发中,原则上来说,应该尽量避免,但是在强调用户体验的要求下或开发平台的限制下(如 Silverlight Socket 通讯),我们不得不用多线程。
收藏 0 赞 0 分享

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法

同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
收藏 0 赞 0 分享

asp.net 通用分页显示辅助类(改进版)

在使用ASP.NET编程时,如果不用控件的默认分页功能,想自己搞一个,可以看看本文的asp.net通用分页显示辅助类哦。
收藏 0 赞 0 分享

微软 Visual Studio 2010官方下载地址给大家

昨天VS2010在网上报道都已经发布了,现在今天在网上找到Visual Studio 2010官方下载地址,提供给大家下载。
收藏 0 赞 0 分享

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例

近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。
收藏 0 赞 0 分享

ASP.NET 页面刷新的实现方法(包括html,js)

ASP.NET 页面刷新的实现方法,比较全了, 包括html与js下的实现方法。
收藏 0 赞 0 分享

asp.net 无刷新翻页就是这么简单

前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
收藏 0 赞 0 分享
查看更多