在.net中彈出日歷的方法有很多種,這里介紹直接使用.net來實例,我們當然還可以使用js日歷來實例哦,下面我分別簡單舉兩個實例吧。有需要的朋友可以了解一下。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ 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代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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語句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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:
1
2
|
ButtonBeginDate.Attributes.Add( "onclick" , "openModeBegin()" ); ButtonEndDate.Attributes.Add( "onclick" , "openModeEnd()" ); |
CalendarForm2.aspx是個臨時容器,提供框架調用CalendarForm3.aspx的內容,以備關掉日期選擇窗體后無法完成傳值,在其html的Head標記之間應該加入如下語句:
代碼如下:
1
2
3
4
5
|
<script id= "clientEventHandlersJS" language= "javascript" > <!-- function IFRAME1_onblur() {} //--> </script> |
CalendarForm2.aspx.cs文件中亦不需要寫任何代碼,只需在body標記之間加入如下代碼:
代碼如下:
1
2
3
4
5
6
|
<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,還可以在用戶誤選后容易糾正。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。