Page.IsValid always returning true with ValidationGroup and dynamic CustomValidator
I am adding a custom validator to the page programmatically on click of a button, then validating the page and checking the IsValid property of the page. but the IsValid property is always returning true. Please help. here is the code. I need to add custom validator dynamically to show validation messages from business object. I am setting the IsValid property of the custom validator to false, so I expect the IsValid property of the Page to return false as well after validation. can't understand what I am doing wrong here.
protected void Button1_Click(object sender, EventArgs e)
{
var validator = new CustomValidator();
validator.IsValid = false;
validator.ErrorMessage = "The input is invalid";
validator.ValidationGroup = "vgCustom";
Page.Validators.Add(validator);
ValidationSummary1.ValidationGroup = "vgCustom";
Page.Validate("vgCustom");
Label1.Text = Page.IsValid ? "The Page is valid" : "The Page is Invalid";
}
and here is the HTML mark-up
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<b开发者_运维百科ody>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Validate" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
i had the same problem with RequiredFieldValidator (Page.IsValid was always true) i had a panel i wanted to show only when validation is false:
<asp:Panel ID="PanelValidationMessage" CssClass="hide messegeFailed" runat="server">
<p><asp:RequiredFieldValidator ID="RequiredFieldValidatorProductForTransfer"
runat="server"
ValidationGroup="Transfer"
ErrorMessage="Please Select Product for Transfer"
ControlToValidate="DDLProductForTransfer"
InitialValue="0"
SetFocusOnError="true"
Display="Dynamic"></asp:RequiredFieldValidator></p>
</asp:Panel>
it was resolved after i changed the "CausesValidation" attribute of the button from "true" to "false":
<asp:Button ID="BtnTransfer"
runat="server"
Text="Transfer Products"
onclick="BtnTransfer_Click"
ValidationGroup="Transfer"
CausesValidation="false"/>
code behind:
Page.Validate("Transfer");
if (Page.IsValid)
{
PanelValidationMessage.CssClass = "hide messegeFailed";
}
else
{
PanelValidationMessage.CssClass = "show messegeFailed";
}
@James is incorrect you need to add the CausesValidation="true"
to your button.
BaseValidator.Validate Method:
Use the Validate method to perform validation on the associated input control. This method allows you to programmatically perform validation on the input control. The IsValid property is automatically updated with the validation results.
So validator.IsValid
is being reset to its default value (True) when you call Page.Validate("vgCustom")
. Whereas with the ServerValidateEventHandler, your code is setting IsValid on Page.Validate("vgCustom")
instead of letting it be reset to the default. If you move validator.IsValid = false
to after the call to Page.Validate("vgCustom")
, the page should fail to validate as expected.
I prefer to use the following pattern though:
/// <summary>
/// A validator that fails unconditionally. Useful if you need to do
/// validation entirely in the code-behind, yet still integrate with
/// the standard ASP.NET validation framework.
/// </summary>
public class FailValidator : BaseValidator {
protected override bool ControlPropertiesValid() {
// make setting ControlToValidate optional
return true;
}
protected override bool EvaluateIsValid() {
return false;
}
}
You need to add the validationgroup to the button to trigger it
First, I think it is easier to add the validator in your aspx, instead of code-behind. it will be hidden anyways at first.
Second, set OnServerValidate="myControl_OnServerValidate" attribute on the in your aspx. Then, in your code-behind implement myControl_OnServerValidate(): your validation code there can set the validator.IsValid to false. When the Page validates, it will automatically aggregate the value from your validator, and the Page.IsValid will be set to false by the Page implementation
NOTE, client validation runs, and if all goes well on client, then server validation runs (please see https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx#Remarks)
精彩评论