Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2007, 07:08 PM
Authorized User
 
Join Date: Sep 2007
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default Validation controls and postback

Very new to asp.net and would like an explanation of how the following works.

Maybe this is just how .net works but I don't like it :)

I have a one field form that uses a validation control (required field validator) and it works I can not submit the form until something is typed in...cool right!

From what is taken in on my sub_pageLoad function I check for isPostback and then call a sql statement to validate what was typed in the box against what exists in the db (please disregard the possibility for sql injection) this is just for testing with .net

In anycase if the field does not match I am displaying another error in a label that says sorry your credentials are not valid..etc I tried to just assign the text property of the error control and enabled it but that didnt work?

So up to now it works just how I would expect...but here is the issue I have....so if I get past the validator control and then enter an incorrect value in the field...then for some reason I clear out the input and try to login again I get both error messages...and that is the whole point of this posting Why???? How can I tell asp.net that the page should always try and validate itself...not just once.

Code provided below

Thanks

Code:
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then
            'Make sure no validator errors
            Page.Validate()
            If Page.IsValid Then
                'Start by determining the connection string value
                Dim connString As String = _
                    ConfigurationManager.ConnectionStrings("clientsTestConnectionString").ConnectionString

                'Create a SqlConnection instance
                Using myConnection As New SqlConnection(connString)
                    'Specify the SQL query
                    Dim emailToVerify As String = txtEmail.Text
                    Dim sql As String = "SELECT id_client FROM clients where client_email='" & Request("txtEmail") & "'"

                    'Create a SqlCommand instance
                    Dim myCommand As New SqlCommand(sql, myConnection)

                    'Get back a DataSet
                    Dim myDataSet As New DataSet

                    'Open connection
                    myConnection.Open()

                    ' Set an OleDbDataReader to the command's results
                    Dim objDataReader As SqlDataReader
                    objDataReader = myCommand.ExecuteReader()
                    If (objDataReader.HasRows) Then
                        objDataReader.Read()
                        Session("id_client") = objDataReader("id_client")
                    Else 'email address not found in db
                        lblLoginError.Text = "Your email address was not found in the system"
                    End If

                    myConnection.Close()
                End Using
            End If ' end valid
        End If ' end postback
    End Sub
End Class


http://mynameissteve.com
__________________
free wifi | http://localwifisearch.com
web dev | http://arizonawebdevelopment.com
 
Old November 9th, 2007, 10:38 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm this doesn't seem to make sense to me and I can not seem to reproduce the error?

A couple of things:

First: why do you use the page_load event to handle this instead of the buttons Click event handler?
Second: Have you put a hook in your code to step through it so you can see what is going on?

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 10th, 2007, 10:40 AM
Authorized User
 
Join Date: Jul 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear sdi126,
First: It is more logic to write the code that validates the user's credentials in the click event of the login button.
Second: you got the two error messages when you tried to login for the second time because
the first error message was the error message of the required field validator control.
the second error was the text of the label used to display an error message incase the credentials were invalid, this text appeared due to the first time you tried to login where the credentials were invalid and accordingly the text property of the label was assigned the error message, then this label will keep this text due to the property of "EnableViewState".this property is true by default and tells the control to keep its state (like its text) during postbacks to the server.
Third: don't forget that the page will not post back to the server unless all validators controls are valid

 
Old November 10th, 2007, 11:09 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by asmaa
 Dear sdi126,
First: It is more logic to write the code that validates the user's credentials in the click event of the login button.
Please explain how it requires more logic.

By placing the code that he has in his Page_Load event into a button click event handler you can eliminate the need to test for postback since the buttons click event will ONLY be raised if the page is posting back to itself. (Unless of course you were to call it explictly in the Page_Load Event like this: button_click(null, null) or Button_Click(Nothing, Nothing))


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 11th, 2007, 08:01 AM
Authorized User
 
Join Date: Jul 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by dparsons
 
Quote:
quote:Originally posted by asmaa
Quote:
 Dear sdi126,
First: It is more logic to write the code that validates the user's credentials in the click event of the login button.
Please explain how it requires more logic.

By placing the code that he has in his Page_Load event into a button click event handler you can eliminate the need to test for postback since the buttons click event will ONLY be raised if the page is posting back to itself. (Unless of course you were to call it explictly in the Page_Load Event like this: button_click(null, null) or Button_Click(Nothing, Nothing))


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
Hello dparsons,
I mean it is better to write down this code in the event handler of the Login button instead of in Page_load but not in both. Also I think there is no need to execute this code whenever the page posts back especially if the page may post back due to any other action rather than the login button.

 
Old November 11th, 2007, 12:08 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Ok I misunderstood what you were saying. I thought you were saying that moving the code that he has in his page load to a button click event handler would require more logic when, in fact, you were suggesting that he move the code to the said event handler.

Thanks for clearing that up.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation\Postback bug TheNinthPlayer ASP.NET 3.5 Basics 1 June 6th, 2008 01:00 AM
form postback and validation srochas .NET Framework 2.0 5 July 17th, 2007 12:23 AM
Validation control and postback Chaotix ASP.NET 1.0 and 1.1 Basics 0 July 18th, 2006 12:31 PM
Validation Controls andyj00 ASP.NET 1.0 and 1.1 Professional 3 June 9th, 2005 09:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.