以下是DatePicker.ascx <%@ Control Language="c#" AutoEventWireup="false" Codebehind="DatePicker.ascx.cs" Inherits="WebApp.DatePicker" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> <FONT id="FONT1" face="宋体" runat="server"> <DIV style="WIDTH: 153px; POSITION: relative; HEIGHT: 20px" ms_positioning="GridLayout" runat="server"> <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 0px" runat="server" Width="128px" Height="24px"></asp:TextBox> <asp:Calendar id="Calendar1" style="Z-INDEX: 102; LEFT: 0px; POSITION: absolute; TOP: 24px" runat="server" Width="152px" Height="48px" BorderWidth="1px" BackColor="#FFFFCC" DayNameFormat="FirstLetter" ForeColor="#663399" Font-Size="8pt" Font-Names="Verdana" BorderColor="#FFCC66" ShowGridLines="True" Visible="False"> <TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle> <SelectorStyle BackColor="#FFCC66"></SelectorStyle> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle> <DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle> <SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle> <TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle> <OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle> </asp:Calendar> <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 0px" runat="server" Width="24px" Text="∨" Height="24px" Font-Size="XX-Small"></asp:Button></DIV> </FONT>
以下是DatePicker.ascx.cs using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace WebApp { public class DatePicker:System.Web.UI.UserControl ,System.Web.UI.INamingContainer { protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.HtmlControls.HtmlGenericControl FONT1; protected System.Web.UI.WebControls.Calendar Calendar1; public string ShortDate { get {return this.TextBox1.Text;} set {this.TextBox1.Text = value;} } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged); this.Button1.Click += new System.EventHandler(this.Button1_Click); } #endregion
private void Button1_Click(object sender, System.EventArgs e) { if(this.Calendar1.Visible == false) { this.Calendar1.Visible = true; this.Button1.Text = "∧"; } else { this.Calendar1.Visible = false; this.Button1.Text = "∨"; this.TextBox1.Text = this.Calendar1.SelectedDate .ToShortDateString(); } }
private void Calendar1_SelectionChanged(object sender, System.EventArgs e) { this.TextBox1.Text = this.Calendar1.SelectedDate .ToShortDateString(); this.Calendar1.Visible = false ; this.Button1.Text = "∨"; } } }
以下是WebForm1.aspx ,测试用 <%@ Register TagPrefix="uc1" TagName="DatePicker" Src="DatePicker.ascx" %> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="dotnet.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"> <DIV style="Z-INDEX: 101; LEFT: 40px; WIDTH: 176px; POSITION: absolute; TOP: 32px; HEIGHT: 25px" ms_positioning="GridLayout"> <uc1:DatePicker id="DatePicker1" runat="server"></uc1:DatePicker></DIV> <asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox> <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 80px" runat="server" Text="Button"></asp:Button></FONT> </form> </body> </HTML> 以下是WebForm1.aspx.cs
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace dotnet { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 }
#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void Button1_Click(object sender, System.EventArgs e) { this.TextBox1.Text = this.Page.Controls[0].FindControl("Datepicker1").FindControl("TextBox1"))).Text; } } }

|