What is the relationship between .IsValid and args.IsValid pages

Are page.IsValid and args.IsValid dependent on each other?

I mean, if args.IsValid = "true", then page.IsValid should be "true".

And, of course, vice versa: if Page.IsValid = "true", so args.IsValid = "true"

+4
source share
2 answers

Page.IsValid (read-only) returns true on successful page validation; otherwise false.

Read the MSDN Document

For this property to return true, all validation controls in the current validation group must be successful. You should check this property only after you invoke the page. Validate method or set the CausesValidation property to true in the OnServerClick event handler for the ASP.NET server control that initiates form processing.

The ServerValidation handler; You can set the property true or false in ServerValidateEventArgument.IsValid if the input check (args.Value) is successful or unsuccessful.

MSDN Document

Once your validation procedure completes, use the IsValid property to indicate whether the value specified in the Validation property has been passed. This value determines whether input control using the CustomValidator control is validated.

+1
source

Both are completely different in implementation. page.IsValid is used for the page and args.isValid for the purpose of custom validation see example below

 <%@ Page Language="C#" AutoEventWireup="True" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>CustomValidator ServerValidate Example</title> <script runat="server"> void ValidateBtn_OnClick(object sender, EventArgs e) { // Display whether the page passed validation. if (Page.IsValid) { Message.Text = "Page is valid."; } else { Message.Text = "Page is not valid!"; } } void ServerValidation(object source, ServerValidateEventArgs args) { try { // Test whether the value entered into the text box is even. int i = int.Parse(args.Value); args.IsValid = ((i%2) == 0); } catch(Exception ex) { args.IsValid = false; } } </script> </head> <body> <form id="form1" runat="server"> <h3>CustomValidator ServerValidate Example</h3> <asp:Label id="Message" Text="Enter an even number:" Font-Names="Verdana" Font-Size="10pt" runat="server" AssociatedControlID="Text1"/> <br /> <asp:TextBox id="Text1" runat="server" /> &nbsp;&nbsp; <asp:CustomValidator id="CustomValidator1" ControlToValidate="Text1" Display="Static" ErrorMessage="Not an even number!" ForeColor="green" Font-Names="verdana" Font-Size="10pt" OnServerValidate="ServerValidation" runat="server"/> <br /> <asp:Button id="Button1" Text="Validate" OnClick="ValidateBtn_OnClick" runat="server"/> </form> </body> </html> 
+2
source

Source: https://habr.com/ru/post/1394326/


All Articles