Page.IsValid conditional not working
-
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
-
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
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 IfI hope this helps. Ryan
-
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
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 !
-
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 IfI hope this helps. Ryan
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.PageProtected 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?!
-
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.PageProtected 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?!
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.
-
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.