Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Doesn't CausesValidation force validation after page load?

Goal

To get validation to occur without me having to actually initiate it.

Code

I markup an input element with this user control:

<vbp:DataRowTextBox ID="SecurityAnswer" runat="server" IsRequired="true"
    RequiredErrorMessage="Please enter an answer for your security question." MaxLength="50"
    Label="Your Answer" />

who's inner markup is like this:

<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" />

who's code for turning validator's on and off looks like this in the Page_Load of the user control:

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;
}

So, in this example I'm turning the required validator on because I have the IsRequired property set to true. However, when I markup a LinkButton like this:

<asp:LinkButton CssClass="actionButton" ID="Submit" runat="server" OnClick="Submit_Click" ValidationGroup="LHError">Change Security Question</asp:LinkButton>

Current Outcome

I'm forced to call Validate to get validation to occur. What am I doing wrong? I thought with CausesValidation set to true (which is the default for a LinkButton) and a ValidateGroup defined, it would run validation automatically after the page load.

And to be very succinct, in the button's click event, even with invalid data, IsValid is true until I call Validate.

Upvotes: 0

Views: 951

Answers (2)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

The answer to this question was in fact quite straight forward. I built a test application with just an ASP.NET TextBox, RequiredFieldValidator, and LinkButton. It performed as I expected, validation was executed during the post back automatically. So, after that I kind of knew what the problem was -I was doing all the dynamic control work in Page_Load but needed to get it done in Init so I overrode OnInit as shown below and all works as expected:

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;
}

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34844

No, the CausesValidation is strictly for client-side validation; server-side validation must be checked via the Page.IsValid property.

Upvotes: 1

Related Questions