2013-06-19 45 views
0

目标

要验证发生而不必实际启动它。页面加载后不会导致验证强制验证?

代码

我标记的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="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:RegularExpressionValidator ID="regexValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:CompareValidator ID="compareValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:CustomValidator ID="customValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:RangeValidator ID="rangeValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" 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定义的,它会在页面加载后自动运行验证。

而且非常简洁,按钮的点击事件,甚至无效数据IsValidtrue直到我打电话Validate

回答

0

这个问题的答案其实很简单。我用ASP.NET TextBoxRequiredFieldValidatorLinkButton构建了一个测试应用程序。它按我的预期执行,自动在回发期间执行验证。所以,在那之后我种知道的问题是 - 我正在做的所有动态控制工作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; 
} 
1

不,原因验证严格用于客户端验证;服务器端验证必须通过Page.IsValid属性进行检查。

+0

我明白我需要检查'IsValid'属性,但是我正在讨论必须自己实例化'Validate'方法。我期待ASP.NET为我完成的工作是我的观点。 –