ASP.NET中實現彈出日曆樣本_實用技巧

來源:互聯網
上載者:User

在.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 Form設計器產生的程式碼 override protected void OnInit(EventArgs e) {  //  // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。  //  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,還可以在使用者誤選後容易糾正。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.