Page.IsValid behavior

D

Drunken Coder

I have a web page with 1 text box, a required field validator a label and a
validation summary. The form is designed so populate the label with text
depending on the state of page.isvalid. However the page will only populate
the label with text if the Page.IsValid but will populate the validation
summary with errors when there is an error. Below is my code. Please someone
point me in the right direction I am starting to pull out my hair over this.

Public Class NewUser

Inherits SDCBase

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox

Protected WithEvents btnAccept As System.Web.UI.WebControls.Button

Protected WithEvents lblErrors As System.Web.UI.WebControls.Label

Protected WithEvents reqUserName As
System.Web.UI.WebControls.RequiredFieldValidator

Protected WithEvents valErrors As
System.Web.UI.WebControls.ValidationSummary

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

End Sub

Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAccept.Click

If Page.IsValid Then

lblErrors.Text = "There are not errors."

Response.Write(Page.IsValid.ToString)

Else

lblErrors.Text = "Please fix the following errors to continue:"

Response.Write(Page.IsValid.ToString)

End If

End Sub

End Class
 
H

Hans Kesting

Drunken Coder said:
I have a web page with 1 text box, a required field validator a label and a
validation summary. The form is designed so populate the label with text
depending on the state of page.isvalid. However the page will only populate
the label with text if the Page.IsValid but will populate the validation
summary with errors when there is an error. Below is my code. Please someone
point me in the right direction I am starting to pull out my hair over this.

What is the problem with that? It sounds to me like everything behaves as
required.
Or am I missing something?
The function of a validationsummary is to collect all validator errors.

Hans Kesting
 
D

Drunken Coder

The "ELSE" portion of the Page.IsValid is not firing.

Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAccept.Click

If Page.IsValid Then

lblErrors.Text = "There are not errors."

Response.Write(Page.IsValid.ToString)

Else

lblErrors.Text = "Please fix the following errors to continue:"

Response.Write(Page.IsValid.ToString)

End If

End Sub

I don't understand why the "ELSE" piece of this code will not fire.
 
H

Hans Kesting

Drunken Coder said:
The "ELSE" portion of the Page.IsValid is not firing.

Ah, *that's* what I missed :)

A validator usually not only works server-side, but also client-side.
If the RequiredFieldValidator notices that the reuired field is empty,
then the postback is prevented, so your code doesn't get called.

It is possible to switch client-side validation off. (property
EnableClientScript
should then be set to false). Then your server-side code should work.


Hans Kesting
 
D

Drunken Coder

I understand what you are saying but if you go to any example of the
RequiredFieldValidator this is the code they use. They check for the
Page.IsValid condition and then post the results of that check to the label
control. I had this working properly 2 days ago but blundered and deleted
the page I was working on.
 
P

Peter Blum

It still sounds like client-side validation is involved here, preventing the
page from being posted back unless everything is valid. If you say "I have
set EnableClientScript to false" or you are not using an IE browser (which
is the only one to support client side validation), then we should be
looking at the server side. I encourage you to use a debugger and put a
breakpoint at Page_Load to see if its hit. If it doesn't run when you have
an error, the page isn't being posted back.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top