页面加载后没有CauseValidation强制验证吗?

时间:2013-06-19 15:24:16

标签: c# asp.net .net

目标

在不必实际启动验证的情况下进行验证。

代码

我使用此用户控件标记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定义,它会在页面加载后自动运行验证。

并且非常简洁,在按钮的点击事件中,即使数据无效IsValid也是true,直到我致电Validate

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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;
}