strange postback problem

C

COHENMARVIN

I have a form with a Submit button. The user fills out the form,
clicks the submit button, and then the page posts back to itself.
Every time the page posts back to itself, the "Page_Load" event fires.
So if the user first types the URL of the page in the browser, the
"Page_Load" event fires. Then he hits submit, and the form contents
are posted back, so the "Page_Load" event fires again. The form
appears again (I clear its contents), and he can fill it again, click
submit, and the "Page_Load" event fires again.
I also have a validator control that gives an error message if the user
leaves a field blank. So if the user clicks submit without filling in
a field, the submit code checks "If Page.IsValid()" and puts an error
message on the page. I find that the "Page_Load" event is fired for
this too. In other words, if the form is incorrect and the user clicks
submit, the "Page_Load" event fires, probably just to put the error
message on the page.
Now in the Page_Load event, I want to store the form's contents in a
database. To do that, I have to know why Page_Load is firing. I don't
want to store the form contents if Page.IsPostBack is False, because
that means this is the first time the page is viewed, and the form
hasn't been filled out yet. I also don't want to store the form
contents if there is an error on the page, even though Page_Load is
fired in this case as well.
So I thought the easy way to to this would be to create a Session
variable that tells me if the submit button found the form fields to be
valid. The submit code would look like this:
''''''''''''''''''''''''''''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.Text = "IS VALID"
Session("Valid") = True
Else
LabDiagnostic.Text = "NOT VALID"
Session("Valid") = False
End If
If Session("Valid") = True Then
LabDiagnostic2.Text = "Session('Valid') = True"
Else
LabDiagnostic2.Text = "Session('Valid') = False"
End If
End Sub
Notice that I put in Diagnostics just to be sure the code is working.
The diagnostics do show that it works.
Then in the "Page_Load" event, I test Session("Valid") again:
'''''''''''''''''''''''''''''''''
Sub Page_Load(Source As Object, E As EventArgs)

If Page.IsPostBack Then
If Session("Valid") = True Then
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
Else
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
End If
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''
But this time the diagnostic gives the opposite result than I expect!
This is so strange that I feel I must be doing something obviously
wrong, but I can't tell what it is.
Here is the entire code of the page, in case someone wants to try it
out:
--- Thanks
--- Marvin

<%@Page Language="VB" debug="true" %>
<%@Import NameSpace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<HTML>
<HEAD>
<TITLE>Enter Passenger Details</TITLE>
</HEAD>

<script language="VB" runat="server">
'''''''''''''''''''''''''''''''''
Sub Page_Load(Source As Object, E As EventArgs)

If Page.IsPostBack Then
If Session("Valid") = True Then
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
Else
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
End If
End If
End Sub
''''''''''''''''''''''''''''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.Text = "IS VALID"
Session("Valid") = True
Else
LabDiagnostic.Text = "NOT VALID"
Session("Valid") = False
End If
If Session("Valid") = True Then
LabDiagnostic2.Text = "Session('Valid') = True"
Else
LabDiagnostic2.Text = "Session('Valid') = False"
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
</script>

<BODY>
<form METHOD="POST" name="MyForm" runat="server">
<ASP:Label id="LabDiagnostic" runat="server" />
<hr>
<ASP:Label id="LabDiagnostic2" runat="server" />
<hr>
<ASP:Label id="LabDiagnostic3" runat="server" />
<center>
<table border="1">
<tr>
<td align="right">First Name</td>
<td><ASP:Textbox id="FirstName" MaxLength="40" Columns="40"
runat="server" />
<ASP:RequiredFieldValidator id="rfvFirstName" runat="server"
ControlToValidate="FirstName"
ErrorMessage="* You must enter a first name."
Display="dynamic">
*
</ASP:RequiredFieldValidator>

</td>
</tr>
<tr><td align="center" colspan="2">
<asp:Button id="MyButton" Text="Submit" runat="server"
OnClick="SubmitClick" />
</td></tr>

</table>
</center>
<center>
<table><tr><td>
<ASP:ValidationSummary id="valSummary" runat="server"
HeaderText = "<b>The following errors were found:</b>"
DisplayMode="List" ShowSummary="True" ShowMessageBox="True" />
<!-- normally I have ShowSummary="False" -->
</td></tr></table>
</center>
</form>
</BODY>
</HTML>
 
M

Mr Newbie

The Page_Load event allways fires. When the page loads off it goes, every
time, no exception.
Now in the Page_Load event, I want to store the form's contents in a
database. To do that, I have to know why Page_Load is firing. I don't
want to store the form contents if Page.IsPostBack is False, because
that means this is the first time the page is viewed, and the form
hasn't been filled out yet. I also don't want to store the form
contents if there is an error on the page, even though Page_Load is
fired in this case as well.
So I thought the easy way to to this would be to create a Session
variable that tells me if the submit button found the form fields to be
valid. The submit code would look like this:

This is not that difficult really. The validators which operate client side
are your first attempt at ensuring that the data is consistant with what you
would like the user to do with it. The validators on the client side will
stop form submission if there criteria is not met.

When the page loads you can test to see if it is a postback and then further
validate from there.

if Page.IsPostback Then

if MyValidationFunction() Then

'Store My Data

Else

litMessage.Text = "Your form has an error on it . . . Blah Blah Blah "

End If

End If
 
C

COHENMARVIN

It seems that my problem was that I assumed the "Submit" button fires
its event before the "Page_Load" event. I had a form with a "Submit"
button, and I thought that when I clicked the "submit" button, the
first thing that would happen was that the code in the "submit" button
event would be executed, and only after that would a "Page_Load" be
executed. But the converse is true, when you click a "submit" button,
the first thing that happens is a "Page_Load", and only after that does
the server execute the code of the "submit" button event.
This is true using the "OnClick" event, the "OnServerClick" event
appears to be radically different, I don't know why.
-- Marvin
 

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

Top