自动具有水印和日历选择功能的文本框
这是我在项目中做的一个自定义控件.
using System; using System.Web.UI; using System.Web.UI.WebControls; namespace WorkflowCenter.Abstraction { ///这个文本框自动具有水印功能和日历选择功能 ///作者:陈希章 ///要求:需要AJAXToolkit支持 public class DataTimeTextBox : Control, IPostBackDataHandler { protected override void OnInit(EventArgs e) { base.OnInit(e); Page.RegisterRequiresControlState(this); Page.RegisterRequiresPostBack(this); } protected override object SaveControlState() { return Date; } protected override void LoadControlState(object savedState) { if (savedState == null) return; Date = (DateTime)savedState; } protected TextBox tx; protected AjaxControlToolkit.CalendarExtender ex; protected AjaxControlToolkit.TextBoxWatermarkExtender WatermarkTextBox; protected RequiredFieldValidator rv; protected CompareValidator cv; protected override void CreateChildControls() { Controls.Clear(); //添加四个控件 tx = new TextBox(); string id = this.ID + "_DateTextBox"; tx.ID = id; ex = new AjaxControlToolkit.CalendarExtender(); ex.ID = this.ID + "_CalendarExtender"; ex.TargetControlID = tx.ID; ex.Format = "yyyy-MM-dd"; WatermarkTextBox = new AjaxControlToolkit.TextBoxWatermarkExtender(); WatermarkTextBox.ID = this.ID + "_WaterMark"; WatermarkTextBox.TargetControlID = tx.ID; WatermarkTextBox.WatermarkText = "请选择日期"; WatermarkTextBox.WatermarkCssClass = "Watermark"; rv = new RequiredFieldValidator(); rv.ID = this.ID + "_RequiredValidator"; rv.ControlToValidate = tx.ID; rv.ErrorMessage = "请输入日期"; rv.Text = "*"; rv.Display = ValidatorDisplay.Dynamic; cv = new CompareValidator(); cv.ID = this.ID + "_CompareValidator"; cv.ErrorMessage = "请输入一个合法的日期"; cv.Text = "*"; cv.Display = ValidatorDisplay.Dynamic; cv.ControlToValidate = tx.ID; cv.Operator = ValidationCompareOperator.DataTypeCheck; cv.Type = ValidationDataType.Date; this.Controls.Add(tx); this.Controls.Add(ex); this.Controls.Add(WatermarkTextBox); this.Controls.Add(rv); this.Controls.Add(cv); ClearChildViewState(); } public DateTime Date { get; set; } #region IPostBackDataHandler 成员 public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) { EnsureChildControls(); DateTime temp; if (DateTime.TryParse(postCollection[tx.UniqueID], out temp)) Date = temp; return true; } public void RaisePostDataChangedEvent() { } #endregion } }
页面中使用的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Register Assembly="WorkflowCenter.Abstraction" Namespace="WorkflowCenter.Abstraction" TagPrefix="workflow" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .Watermark { color: Gray; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="s" runat="server"> </asp:ScriptManager> <asp:ValidationSummary ID="sum" runat="server" /> <workflow:DataTimeTextBox ID="date" runat="server"></workflow:DataTimeTextBox> <asp:Button ID="b" runat="server" Text="测试" OnClick="b_Click" /> </div> </form> </body> </html>