Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Page.IsValid conditional not working

Page.IsValid conditional not working

Scheduled Pinned Locked Moved ASP.NET
questionhtmldesignsysadminhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bigmish2
    wrote on last edited by
    #1

    Newbie question here: I’ve got a page with a text box with a requered field validator and a label that should say “OK” if there is text in the field (using Page.IsValid) or “Bad” if not. However, it doesn’t say “Bad” when there is no text. Below is the page code and the source code. Can anyone help me under stand why this is happening? Thanks! PAGE CODE: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="pageIsValidTest.aspx.vb" Inherits="pageIsValidTest" %> <!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 runat="server">       <title>Untitled Page</title> </head> <body>       <form id="form1" runat="server">       <div>              Textbox:             <asp:TextBox ID="TextBox" runat="server"></asp:TextBox>             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox"                   ErrorMessage="enter text"></asp:RequiredFieldValidator><br />             <asp:Button ID="Button1" runat="server" Text="Button" /><br />             Label:             <asp:Label ID="Label" runat="server"></asp:Label></div>       </form> </body> </html> SOURCE CODE Partial Class pageIsValidTest       Inherits System.Web.UI.Page       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click             If Page.IsValid Then                   Label.Text = "OK"             Else                   Label.Text = "Bad"             End If       End Sub End Class

    R S 2 Replies Last reply
    0
    • B bigmish2

      Newbie question here: I’ve got a page with a text box with a requered field validator and a label that should say “OK” if there is text in the field (using Page.IsValid) or “Bad” if not. However, it doesn’t say “Bad” when there is no text. Below is the page code and the source code. Can anyone help me under stand why this is happening? Thanks! PAGE CODE: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="pageIsValidTest.aspx.vb" Inherits="pageIsValidTest" %> <!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 runat="server">       <title>Untitled Page</title> </head> <body>       <form id="form1" runat="server">       <div>              Textbox:             <asp:TextBox ID="TextBox" runat="server"></asp:TextBox>             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox"                   ErrorMessage="enter text"></asp:RequiredFieldValidator><br />             <asp:Button ID="Button1" runat="server" Text="Button" /><br />             Label:             <asp:Label ID="Label" runat="server"></asp:Label></div>       </form> </body> </html> SOURCE CODE Partial Class pageIsValidTest       Inherits System.Web.UI.Page       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click             If Page.IsValid Then                   Label.Text = "OK"             Else                   Label.Text = "Bad"             End If       End Sub End Class

      R Offline
      R Offline
      RyanMorris
      wrote on last edited by
      #2

      Hi BigMish2, You need to validate the page before you can tell if it's valid:

      Page.Validate()
      If Page.IsValid Then
      Label.Text = "OK"
      Else
      Label.Text = "Bad"
      End If

      I hope this helps. Ryan

      B 1 Reply Last reply
      0
      • B bigmish2

        Newbie question here: I’ve got a page with a text box with a requered field validator and a label that should say “OK” if there is text in the field (using Page.IsValid) or “Bad” if not. However, it doesn’t say “Bad” when there is no text. Below is the page code and the source code. Can anyone help me under stand why this is happening? Thanks! PAGE CODE: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="pageIsValidTest.aspx.vb" Inherits="pageIsValidTest" %> <!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 runat="server">       <title>Untitled Page</title> </head> <body>       <form id="form1" runat="server">       <div>              Textbox:             <asp:TextBox ID="TextBox" runat="server"></asp:TextBox>             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox"                   ErrorMessage="enter text"></asp:RequiredFieldValidator><br />             <asp:Button ID="Button1" runat="server" Text="Button" /><br />             Label:             <asp:Label ID="Label" runat="server"></asp:Label></div>       </form> </body> </html> SOURCE CODE Partial Class pageIsValidTest       Inherits System.Web.UI.Page       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click             If Page.IsValid Then                   Label.Text = "OK"             Else                   Label.Text = "Bad"             End If       End Sub End Class

        S Offline
        S Offline
        Samer Aburabie
        wrote on last edited by
        #3

        Hi there, What you are doing is a server validation using the Validators set on the page, and Page.IsValid property returns a value according to wether a certain Validation Group is valid or not OR all validation controls are valid or not on the page. In other words checking its value immediatley will be with no value for you, you have to call Page.Validate(validationGroupName) first in order for this value to return the desired and good value of it. You can call Page.Validate() to validate all validators on the page, or call Page.Validate(validationGroupName) if you have a group, you can set a group of validation on the controls using the ValidationGroup property if you wanted. Hope that helps.

        Sincerely Samer Abu Rabie Imagination is more important than knowledge !

        1 Reply Last reply
        0
        • R RyanMorris

          Hi BigMish2, You need to validate the page before you can tell if it's valid:

          Page.Validate()
          If Page.IsValid Then
          Label.Text = "OK"
          Else
          Label.Text = "Bad"
          End If

          I hope this helps. Ryan

          B Offline
          B Offline
          bigmish2
          wrote on last edited by
          #4

          Thanks Ryan but unfortunately that did not work. Update: interestingly I get the same behavior as with the code I posted before with the following source code (meaning that Label only displays “OK” if I put text in the text box even when I comment out the conditional!):

          Partial Class pageIsValidTest
          Inherits System.Web.UI.Page

          Protected Sub Button1\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
              'If Page.IsValid Then
              Label.Text = "OK"
              'Else
              'Label.Text = "Bad"
              'End If
          End Sub
          

          End Class

          What is going on?!

          R 1 Reply Last reply
          0
          • B bigmish2

            Thanks Ryan but unfortunately that did not work. Update: interestingly I get the same behavior as with the code I posted before with the following source code (meaning that Label only displays “OK” if I put text in the text box even when I comment out the conditional!):

            Partial Class pageIsValidTest
            Inherits System.Web.UI.Page

            Protected Sub Button1\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                'If Page.IsValid Then
                Label.Text = "OK"
                'Else
                'Label.Text = "Bad"
                'End If
            End Sub
            

            End Class

            What is going on?!

            R Offline
            R Offline
            RyanMorris
            wrote on last edited by
            #5

            I'm not sure why it wouldn't work after you'd added the Page.Validate()line, does it give an error? You've commented out the condition the condition, but you haven't commented out Label.Text = "OK" so every time you click the button it will set the labels text to OK regardless of what you do on the page.

            B 1 Reply Last reply
            0
            • R RyanMorris

              I'm not sure why it wouldn't work after you'd added the Page.Validate()line, does it give an error? You've commented out the condition the condition, but you haven't commented out Label.Text = "OK" so every time you click the button it will set the labels text to OK regardless of what you do on the page.

              B Offline
              B Offline
              bigmish2
              wrote on last edited by
              #6

              No no error, it just dosn't display "Bad" if I leave the field blank. I wasn't clear: it only says "OK" if there is text in the feild!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups