abstract:在.net中彈出日歷的方法有很多種,這里介紹直接使用.net來(lái)實(shí)例,我們當(dāng)然還可以使用js日歷來(lái)實(shí)例哦,下面我分別簡(jiǎn)單舉兩個(gè)實(shí)例吧。有需要的朋友可以了解一下。代碼如下:<%@ Control Language="c#" AutoEventWireup="false" Codebehind="ctlCal
在.net中彈出日歷的方法有很多種,這里介紹直接使用.net來(lái)實(shí)例,我們當(dāng)然還可以使用js日歷來(lái)實(shí)例哦,下面我分別簡(jiǎn)單舉兩個(gè)實(shí)例吧。有需要的朋友可以了解一下。
代碼如下:
<%@ 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 的摘要說(shuō)明。 /// </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) { // 在此處放置用戶代碼以初始化頁(yè)面 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 窗體設(shè)計(jì)器生成的代碼 override protected void OnInit(EventArgs e) { // // CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器 /// 修改此方法的內(nèi)容。 /// </summary> private void InitializeComponent() { this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged); this.Load += new System.EventHandler(this.Page_Load); } #endregion #region 日歷選擇時(shí)的事件 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 } }
好了下面結(jié)果js+.net實(shí)現(xiàn)彈出日歷
在需要調(diào)用日期選擇的頁(yè)面放置兩個(gè)TEXTBOX與BUTTON以選擇開(kāi)始時(shí)間與結(jié)束時(shí)間,并在html代碼的 </body>之前加入如下
javascript語(yǔ)句:
<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>
以上語(yǔ)句定義了兩個(gè)模態(tài)對(duì)話框,當(dāng)調(diào)用模態(tài)對(duì)話框時(shí)打開(kāi)CalendarForm2.aspx頁(yè)面選擇日期,本頁(yè)面窗體FORM名稱(chēng)為Form1,兩個(gè)TextBox分別接收傳遞進(jìn)來(lái)的兩個(gè)時(shí)間值而且應(yīng)該能互不影響。注意html中窗體的定義應(yīng)該與javascript中定義的對(duì)應(yīng)并且應(yīng)該是服務(wù)器端運(yùn)行的,如<form id="Form1" method="post" runat="server">。
在本頁(yè)面WebForm1.aspx.cs代碼部分頁(yè)面加載Page_Load事件內(nèi)加入如下語(yǔ)句將定義的javascript行為賦予Button:
ButtonBeginDate.Attributes.Add("onclick", "openModeBegin()"); ButtonEndDate.Attributes.Add("onclick", "openModeEnd()");
CalendarForm2.aspx是個(gè)臨時(shí)容器,提供框架調(diào)用CalendarForm3.aspx的內(nèi)容,以備關(guān)掉日期選擇窗體后無(wú)法完成傳值,在其html的Head標(biāo)記之間應(yīng)該加入如下語(yǔ)句:
代碼如下:
<script id="clientEventHandlersJS" language="javascript"> <!-- function IFRAME1_onblur() {} //--> </script>
CalendarForm2.aspx.cs文件中亦不需要寫(xiě)任何代碼,只需在body標(biāo)記之間加入如下代碼:
代碼如下:
<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我們實(shí)際用到的日期選擇頁(yè)面包含一個(gè)日歷控件與一個(gè)Button一個(gè)TextBox,此處直接將日歷控件Calendar的選定值傳給第一個(gè)頁(yè)面WebForm1.aspx更簡(jiǎn)單,但我們沒(méi)有這樣做,不直接傳值主要是考慮到大多數(shù)用戶的使用習(xí)慣,在此將日歷控件選中的值傳給頁(yè)面上的TextBox,按下Button后再傳給WebForm1.aspx,還可以在用戶誤選后容易糾正。
更多關(guān)于ASP.NET中實(shí)現(xiàn)彈出日歷示例請(qǐng)關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!