溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET中如何實(shí)現(xiàn)彈出日歷

發(fā)布時間:2021-08-26 15:39:57 來源:億速云 閱讀:223 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹ASP.NET中如何實(shí)現(xiàn)彈出日歷,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

代碼如下:

<%@ 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" >
 <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 窗體設(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 日歷選擇時的事件
 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)用日期選擇的頁面放置兩個TEXTBOX與BUTTON以選擇開始時間與結(jié)束時間,并在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>

以上語句定義了兩個模態(tài)對話框,當(dāng)調(diào)用模態(tài)對話框時打開CalendarForm2.aspx頁面選擇日期,本頁面窗體FORM名稱為Form1,兩個TextBox分別接收傳遞進(jìn)來的兩個時間值而且應(yīng)該能互不影響。注意html中窗體的定義應(yīng)該與javascript中定義的對應(yīng)并且應(yīng)該是服務(wù)器端運(yùn)行的,如<form id="Form1" method="post" runat="server">。

在本頁面WebForm1.aspx.cs代碼部分頁面加載Page_Load事件內(nèi)加入如下語句將定義的javascript行為賦予Button:
  

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

CalendarForm2.aspx是個臨時容器,提供框架調(diào)用CalendarForm3.aspx的內(nèi)容,以備關(guān)掉日期選擇窗體后無法完成傳值,在其html的Head標(biāo)記之間應(yīng)該加入如下語句:

代碼如下:

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

CalendarForm2.aspx.cs文件中亦不需要寫任何代碼,只需在body標(biāo)記之間加入如下代碼: 

代碼如下:

<body runat="server" ID="Body1"> 
 <form id="Form1" method="post" runat="server"> 
  <iframe frameborder="no" src='CalendarForm3.aspx'  id="IFRAME1" 
  language="javascript" onblur="return IFRAME1_onblur()"></iframe> 
 </form> 
</body>

CalendarForm3.aspx我們實(shí)際用到的日期選擇頁面包含一個日歷控件與一個Button一個TextBox,此處直接將日歷控件Calendar的選定值傳給第一個頁面WebForm1.aspx更簡單,但我們沒有這樣做,不直接傳值主要是考慮到大多數(shù)用戶的使用習(xí)慣,在此將日歷控件選中的值傳給頁面上的TextBox,按下Button后再傳給WebForm1.aspx,還可以在用戶誤選后容易糾正。

以上是“ASP.NET中如何實(shí)現(xiàn)彈出日歷”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI