asp.net中自定义日期控件方法(转载)
本系统中要用到一个日期输入框,日期标题可变(操作日期、申请日期、确认日期、开始日期、截止日期....),可以为空或者必须输入,光标在输入框中时,自动弹出日期选择框。结合这些需求,封装了一个日期选择控件。
asp端代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomDate.ascx.cs" Inherits="Control_StartEndDate" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="txtEndDate" runat="server" onfocus="setday(this)" Width="88px" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEndDate"
Enabled="false" Display="None">
</asp:RequiredFieldValidator>
asp对应的cs代码为:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Control_StartEndDate : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// Label1.Text是动态变化的,所以验证信息也相应动态生成
RequiredFieldValidator1.ErrorMessage = Label1.Text + "不能为空!";
}
// 当前日期字符串值
public string DateString
{
get
{
return txtEndDate.Text;
}
}
// 日期的标题名称(操作日期、申请日期、确认日期、开始日期、截止日期....)
public string DateTitle
{
set
{
//"截止日期";
Label1.Text = value;
}
}
// 日期是否允许为空,即是否启用RequiredFieldValidator
public bool Nullable
{
set
{
//bool nullable = bool.Parse(value);
if (value == true)
RequiredFieldValidator1.Enabled = false;
else
RequiredFieldValidator1.Enabled = true;
}
}
// 初始日期字符串文本
public string InitialText
{
set
{
txtEndDate.Text = value;
}
}
// 初始日期值
public String InitialDate
{
set
{
//if (IsPostBack) return;
string dateStr = "";
DateTime dt = DateTime.Today; // 取当日日期
// 取上一开放日的日期
if ("LastOpenDay".Equals(value))
{
// 为简单起见,上一个开放日取前天(要精确日期则必须查询数据库)
DateTime newdt = dt.AddDays(-2);
dateStr = newdt.ToString("yyyy-MM-dd");
}
else if ("Today".Equals(value))
{
dateStr = dt.ToString("yyyy-MM-dd");
}
else
{
}
txtEndDate.Text = dateStr;
}
}
}
使用范例
<%@ Page Language="C#" MasterPageFile="~/MasterPage/Navigation.master" AutoEventWireup="true"
CodeFile="share4areadis.aspx.cs" Inherits="report_share4birthsex" Theme="Harvest" %>
<%@ Register Src="../Control/FundCodeList.ascx" TagName="FundCodeList" TagPrefix="uc1" %>
<%@ Register Src="../Control/CustomDate.ascx" TagName="CustomDate" TagPrefix="uc2" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<uc1:FundCodeList ID="FundCodeList1" runat="server" ShowCustomItem="true" />
<uc2:CustomDate ID="CustomDate1" runat="server" DateTitle="截止日期" Nullable="false"
InitialDate="LastOpenDay" />
<asp:Button ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList"
ShowMessageBox="true" ShowSummary="false" />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt"
Style="width: 100%; height: 120%">
<LocalReport>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetShare4AreaDis"
TypeName="VM.BusinessRule.CustAnalysis">
<SelectParameters>
<asp:ControlParameter ControlID="FundCodeList1" Name="FundCode" PropertyName="FundCode"
Type="String" />
<asp:ControlParameter ControlID="CustomDate1" Name="EndDate" PropertyName="DateString"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</asp:Content>
具体看<uc2:CustomDate ID="CustomDate1" runat="server" DateTitle="截止日期" Nullable="false"
InitialDate="LastOpenDay" />这行,DateTitle为日期标题,Nullable设置是否允许为空,InitialDate设置初始日期(当天,上一个开放日)
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList"
ShowMessageBox="true" ShowSummary="false" />为验证汇总,将各个控件中的验证信息汇总起来,统一显示(ShowMessageBox="true"表示用弹出消息框的形式显示验证信息)。