473,379 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

strange postback problem

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>

Nov 19 '05 #1
2 2181
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


--
Best Regards

The Inimitable Mr Newbie
"COHENMARVIN" <co*********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...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>

Nov 19 '05 #2
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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dot net work | last post by:
Hi, Make up a very simple project as follows: 1 aspx form 3 web user controls (referred to as A, B, and C) "A" web user control: Put 1 textbox and 1 button on this web user control. (You...
1
by: Kepler | last post by:
I'm fighting a really strange bug that involves both a DataGrid and a Repeater disappearing on postback. The strange thing is that I've distilled the problem down to a simple program, that...
2
by: Tim_Mac | last post by:
hi, i have an aspx page which dynamically loads a user control and adds it to a placeholder. the control is recreated and added to the placeholder for postbacks as well. the user control...
4
by: William Sullivan | last post by:
I have an extremely weird problem that I have no idea how to approach. I have a simple page with a search textbox and a search button. The button causes a postback, where I perform the search and...
1
by: Mossman | last post by:
Hello, I will discuss at a high level what is happening on an ASP.NET web page I developed a year ago in ASP.NET using Visual Studio 2003 and currently support. I have an ASP.NET web page...
5
by: Mr Newbie | last post by:
OK, I have a fairly simple setup where I have an Index of records which are displayed using a datagrid on a page. When you click the link associated with a Row (ArticleID) , this ID is passed to a...
0
by: savvy | last post by:
I'm having some background graphics layout on the page and a table layout on top of it. Inside the table i have two DropDownList menus whose AutoPostBack Property is set to true and I'm using two...
7
by: Tim_Mac | last post by:
hi, using .net 2.0, i have a web form with lots of textboxes, drop-down-lists etc. There are lots of required field validators and regular expression validators. When i click the 'save' button,...
0
by: riteshjain1982 | last post by:
Pagepostback.....strange problem Hi, I am at cient place and getting very strange problem with my asp.net application............when i call my web application from any desktop machine in lan...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.