Why is "Page.IsValid = True' even when "args.IsValid = False"?

Last post 12-04-2008 4:58 PM by xxxd. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 12-04-2008 2:11 AM

    Why is "Page.IsValid = True' even when "args.IsValid = False"?

     Hi Folks,

     I'm getting strange behavior with custom validation controls -
     They all stopped working!
    The Validation Controls do their job...by making args.isValid to False..
     but for some reason Page.isValid remains always true...so in fact it is as though the validators had no effect on the page...

    What can be causing this?

    This is an example of a validator I have on the page with the resulting trace out:
    Validation Event:

     Protected Sub CustomValEClaim_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    ' Trace that event is firring
    Trace.Warn("CustomVal Happening")
    ' Trace Values which Validator must compare
    Trace.Warn("Max " + Session("MaxVal").ToString)
    Trace.Warn("Current " + CurrentVal.Text)

    ' Evaluate True or False
    If CInt(CurrentVal.Text) < Session("MaxVal") Then
    args.IsValid = True
    '
    Trace.Warn("Args Should be True")
    Trace.Warn("args.isValid:" + args.IsValid.ToString)
    Trace.Warn("page.isValid: " + Page.IsValid.ToString)
    Else
    args.IsValid = False
    args.IsValid = False
    Trace.Warn("Should be False")
    Trace.Warn("args.isValid:" + args.IsValid.ToString)
    Trace.Warn("page.isValid: " + Page.IsValid.ToString)
    End If

    End Sub

     TraceOut

    CustomVal Happening   
    Max 10   
    Current 12   
    Should be False   
    args.isValid:False   
    page.isValid: True
         < --- Why is this True..when args.isValid is False?!

     

    Regards,

    - Joel

  •  Advertisement

    Featured Advertisement

     
  • 12-04-2008 8:12 AM In reply to

    • sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 12,211
    • MVP

    Re: Why is "Page.IsValid = True' even when "args.IsValid = False"?

    Can you post the relevant aspx part?

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
  • 12-04-2008 12:20 PM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Slacker
    • Points 19,067

    Re: Why is "Page.IsValid = True' even when "args.IsValid = False"?

     Quoting this page from MSDN:

    After a postback but before your event methods are called, the page calls the validator controls and aggregates their results into the Page.IsValid property. (You can also call the validator controls explicitly using the Validate method.) In your own code, you should check that the Page.IsValid property returns true before processing input. Even though script-enabled browsers might prevent a postback from occurring on the client if a validation check has failed, you should always also check Page.IsValid in server code before processing validated data.

    The Page.IsValid property would be changed according to all validating results after a postback, that is, it stays true in the server validation method call.

     

    Please try out the example give by the page

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx

    So the right place to check if customvalidator result is in the event code, rather than validation function. As in:

     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);
                Response.Write("validation " + Page.IsValid); // will output false
             }

             catch(Exception ex)
             {

                args.IsValid = false;
            

             }

          }

  • 12-04-2008 2:49 PM In reply to

    Re: Why is "Page.IsValid = True' even when "args.IsValid = False"?

     Hi Thanks for the Replies!

    Sonu - This is the relevant asp.net part:

    Enter a number:<asp:TextBox ID="EClaim" runat="server" Text="0" Font-Size="X-Large" Width="40"></asp:TextBox>

    <asp:RegularExpressionValidator ID="EClaimRegex" runat="server" ErrorMessage="Enter only # for value" ControlToValidate="EClaim" ValidationExpression="^\d+$"></asp:RegularExpressionValidator

     <asp:CustomValidator ID="CustomValEClaim" runat="server" ErrorMessage="Error Msg set in codebehind " OnServerValidate="CustomValEClaim_ServerValidate" Display="Dynamic" ControlToValidate="EClaim"></asp:CustomValidator>

    However the problem I am having affects all my custom validators on the page.... so I don't think I had to do with this specific control
    The validators seem do being their job..by making args.isvalid = False...but it seems this is never aggreated to page.isvalid = False.... weird!

    xxd: If I understand what you are saying, is that I should verify the Page.isValid property in the codebehind, and not realy on the clients scripts
    - Yes, but in this case it seems that even when the validations fails..my page.isValid remains True...!!!!

    Regards,

    - Joel

     

     

     

     

     

     

  • 12-04-2008 3:55 PM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Slacker
    • Points 19,067

    Re: Why is "Page.IsValid = True' even when "args.IsValid = False"?

    First, It is perfectly fine to do it on the client side (I often do it on client side ONLY, though not recommended). Second, from the code your posted before, that the Page.IsValid is true is because you did not test the condition after the postback. Try it again in the event code.

    If it does not work, paste the complete yet relevant code, I will check it out.

    The following is my testing code:

    <%@ 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  >
    <head>
        <title>CustomValidator ServerValidate Example</title>

    <script language="javascript">

        function clientValidate(source,args) {
            if (args.Value % 2 == 0)
                args.IsValid = true;
            else
                args.IsValid = false;

            alert(args.IsValid);
        }
       
    </script>
    <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);
                Response.Write("validation " + Page.IsValid);
             }

             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"  ClientValidationFunction="clientValidate"/>

          <br />

          <asp:Button id="Button1"
               Text="Validate"
               OnClick="ValidateBtn_OnClick"
               runat="server"/>

       </form>

    </body>
    </html>

     

  • 12-04-2008 4:54 PM In reply to

    Re: Why is "Page.IsValid = True' even when "args.IsValid = False"?

    Hi, I got it working!

    Ok, so i've done the test that you suggested...
    When I presse the button...the page.isValid= False worked...

    so I realized that the reason that my custom validators were not working was because in my event handler I did not have a
    If Page.IsValid Then
    ' Do Stuff

    else

    ' dont do stuff

    End if

    And since my Custom Validators don't have client script, the page just posted back and resumed i'ts process without realizing that the page was not valid...

    So the bottom line is...when you are using validators ALWAYS use a verification block, before executing an event...
    Frankly that should be a default behavior of the .net framework.... I Page.isNot Valid...don't execute Period!
    Also giving developers the option to overide this default behavior...but the default should be biassed towards security..

    Thanks,

    PolkaDotNet

  • 12-04-2008 4:58 PM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Slacker
    • Points 19,067

    Re: Why is "Page.IsValid = True' even when "args.IsValid = False"?

    Very good point. Thanks 

Page 1 of 1 (7 items)