【发布时间】:2015-03-03 03:49:16
【问题描述】:
我将文本框类扩展如下,以充当 DateTextBox 并验证字段格式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Myapplication.App_Code
{
public class DateTextBox : TextBox
{
private RegularExpressionValidator regexval;
public string InvalidDate="Incorrect date format, must be dd/mm/yyyy";
public string ClientScript = "true";
protected override void OnInit(EventArgs e)
{
regexval = new RegularExpressionValidator();
regexval.ControlToValidate = this.ID;
regexval.ErrorMessage ="'"+this.Text+"'"+ this.InvalidDate;
regexval.EnableClientScript = (this.ClientScript.ToLower() != "false");
regexval.ValidationExpression = "^(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[-/.](19|20)\\d\\d$";
//regexval.Text = "*Incorrect date format";
regexval.ForeColor = System.Drawing.Color.Red;
regexval.Display = ValidatorDisplay.None;
Controls.Add(regexval);
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{
Attributes.Add("placeholder", "dd/mm/yyyy");
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
regexval.RenderControl(writer);
}
}
}
在我的 aspx 中,我有这个 datetextbox 以及其他几个具有开箱即用的必填字段验证器的控件。
这是我的 aspx:
<td>
<RE:LabelExtended runat="server" ID="lblDateOfBirth" Text="Birth Date" Required="True">
</RE:LabelExtended>
<asp:RequiredFieldValidator ID="ReqValDateOfBirth" runat="server" ControlToValidate="txtDateOfBirth" ErrorMessage="Please enter Date of Birth" ForeColor="Red" Display="None" ></asp:RequiredFieldValidator>
</td>
<td>
<RE:DateTextBox runat="server" ID="txtDateOfBirth" Text='<%# Bind("DateOfBirth","{0:dd/MM/yyyy}") %>'></RE:DateTextBox>
</td>
<td>
<asp:ValidationSummary ID="Tab1ValidationSummary" runat="server" ShowSummary="true" ForeColor="Red" Enabled="true" />
</td>
我的问题是我的 DateTextBox 控件中的正则表达式验证器没有在其他控件的验证摘要中显示其错误消息。 知道该怎么做吗?
编辑:
我尝试为页面上的所有控件提供一个验证组,并为控件中的正则表达式提供相同的组,这使得错误消息按要求显示,但我无法修复我的控件中的验证组,因为我有几个页面使用了这个控件。
【问题讨论】: