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>
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>