目标
要验证发生而不必实际启动它。页面加载后不会导致验证强制验证?
代码
我标记的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
。
我明白我需要检查'IsValid'属性,但是我正在讨论必须自己实例化'Validate'方法。我期待ASP.NET为我完成的工作是我的观点。 –