在最近開發的一個asp.net 2.0系統上,要給業務部門出資料,在查詢介面上經常要用到一個日期選擇框,2周前看了www.asp.net上發布的一本書
Asp.net Data Tutorial,對asp.net的三層結構開發方式有了一定瞭解(DataSet, ObjectDataStore)。上周,下了兩個asp.net上的範常式序代碼,一個是Duwamish7,另一個是petshop3.0(j2ee平台上petstore的競爭版)。網上也有這兩個程式的程式碼分析,Duwamish7採用自訂DataSet+sqlconnection封裝預存程序的方式,petshop3.0則採用常見的三層結構,資料訪問層DAL通過ORM映射,將結果表的一行映射成一個對象(比如Customer,Product,Order等,考慮了分頁機制),商務邏輯層BLL處理參數校正等商務邏輯的實現,展現層處理資料的顯示(內容和形式的分離)。看過代碼後,感覺asp.net裡邊的自訂控制項的功能還是很強大的。比起j2ee裡邊的標籤庫,代碼複用程度更高,尤其適合於做各種表單查詢控制項。比如根據商務邏輯,將各種輸入框、下拉框組合起來,進行複用。
本系統中要用到一個日期輸入框,日期標題可變(操作日期、申請日期、確認日期、開始日期、到期日....),可以為空白或者必須輸入,游標在輸入框中時,自動彈出日期選擇框。結合這些需求,封裝了一個日期選擇控制項。
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"表示用彈出訊息框的形式顯示驗證資訊)。