在不必实际启动验证的情况下进行验证。
我使用此用户控件标记input
元素:
<vbp:DataRowTextBox ID="SecurityAnswer" runat="server" IsRequired="true"
RequiredErrorMessage="Please enter an answer for your security question." MaxLength="50"
Label="Your Answer" />
谁的内部标记是这样的:
<input type="text" id="textBox" runat="server" />
<asp:RequiredFieldValidator ID="requiredFieldValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:RegularExpressionValidator ID="regexValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:CompareValidator ID="compareValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:CustomValidator ID="customValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:RangeValidator ID="rangeValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
用于打开和关闭验证器的的代码在用户控件的Page_Load
中如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (this.ClientIDMode == ClientIDMode.Static)
{
this.textBox.ID = this.ID;
}
this.label.Attributes.Add("for", this.textBox.ClientID);
this.label.InnerHtml = string.Format("{0}:{1}", this.Label, EmitRequiredSup());
this.requiredFieldValidator.Visible = this.IsRequired;
this.regexValidator.Visible = (this.Regexes != null);
if (this.regexValidator.Visible)
{
var regexes = string.Join("|", this.Regexes);
this.regexValidator.ValidationExpression = regexes;
}
this.compareValidator.Visible = !string.IsNullOrEmpty(this.ControlToCompare);
this.rangeValidator.Visible = !string.IsNullOrEmpty(this.RangeMinimumValue);
this.requiredFieldValidator.ControlToValidate = this.textBox.ID;
this.regexValidator.ControlToValidate = this.textBox.ID;
this.compareValidator.ControlToValidate = this.ID;
this.customValidator.ControlToValidate = this.textBox.ID;
this.rangeValidator.ControlToValidate = this.textBox.ID;
}
所以,在这个例子中我打开了所需的验证器,因为我将IsRequired
属性设置为true。但是,当我像这样标记LinkButton
时:
<asp:LinkButton CssClass="actionButton" ID="Submit" runat="server" OnClick="Submit_Click" ValidationGroup="LHError">Change Security Question</asp:LinkButton>
我被迫致电Validate
以进行验证。我究竟做错了什么?我认为CausesValidation
设置为true
(这是LinkButton
的默认设置)和ValidateGroup
定义,它会在页面加载后自动运行验证。
并且非常简洁,在按钮的点击事件中,即使数据无效,IsValid
也是true
,直到我致电Validate
。
答案 0 :(得分:1)
不,CausesValidation严格用于客户端验证;必须通过Page.IsValid属性检查服务器端验证。
答案 1 :(得分:0)
这个问题的答案实际上非常直截了当。我只使用ASP.NET TextBox
,RequiredFieldValidator
和LinkButton
构建了一个测试应用程序。它按照我的预期执行,验证在回发期间自动执行。所以,之后我知道问题是什么 - 我正在Page_Load
进行所有动态控制工作,但是需要在Init
完成它,所以我覆盖了OnInit
,如下所示并且所有工作都按预期进行:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.compareValidator.ID = string.Format("{0}_{1}", this.ID, this.compareValidator.ID);
if (this.ClientIDMode == ClientIDMode.Static)
{
this.textBox.ID = this.ID;
}
this.label.Attributes.Add("for", this.textBox.ClientID);
this.label.InnerHtml = string.Format("{0}:{1}", this.Label, EmitRequiredSup());
this.requiredFieldValidator.Visible = this.IsRequired;
this.regexValidator.Visible = (this.Regexes != null);
if (this.regexValidator.Visible)
{
var regexes = string.Join("|", this.Regexes);
this.regexValidator.ValidationExpression = regexes;
}
this.compareValidator.Visible = !string.IsNullOrEmpty(this.ControlToCompare);
this.rangeValidator.Visible = !string.IsNullOrEmpty(this.RangeMinimumValue);
this.requiredFieldValidator.ControlToValidate = this.textBox.ID;
this.regexValidator.ControlToValidate = this.textBox.ID;
this.compareValidator.ControlToValidate = this.ID;
this.customValidator.ControlToValidate = this.textBox.ID;
this.rangeValidator.ControlToValidate = this.textBox.ID;
}