473,748 Members | 6,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(obj Sender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.T ext = "IS VALID"
Session("Valid" ) = True
Else
LabDiagnostic.T ext = "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(Sourc e 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="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>

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

<script language="VB" runat="server">
''''''''''''''' ''''''''''''''' '''
Sub Page_Load(Sourc e 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(obj Sender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.T ext = "IS VALID"
Session("Valid" ) = True
Else
LabDiagnostic.T ext = "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="LabDiagnost ic" runat="server" />
<hr>
<ASP:Label id="LabDiagnost ic2" runat="server" />
<hr>
<ASP:Label id="LabDiagnost ic3" runat="server" />
<center>
<table border="1">
<tr>
<td align="right">F irst Name</td>
<td><ASP:Textbo x id="FirstName" MaxLength="40" Columns="40"
runat="server" />
<ASP:RequiredFi eldValidator id="rfvFirstNam e" runat="server"
ControlToValida te="FirstName"
ErrorMessage="* You must enter a first name."
Display="dynami c">
*
</ASP:RequiredFie ldValidator>

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

</table>
</center>
<center>
<table><tr><t d>
<ASP:Validation Summary id="valSummary " runat="server"
HeaderText = "<b>The following errors were found:</b>"
DisplayMode="Li st" ShowSummary="Tr ue" ShowMessageBox= "True" />
<!-- normally I have ShowSummary="Fa lse" -->
</td></tr></table>
</center>
</form>
</BODY>
</HTML>

Nov 19 '05 #1
2 2198
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 MyValidationFun ction() 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 º¿º
"COHENMARVI N" <co*********@ho tmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(obj Sender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.T ext = "IS VALID"
Session("Valid" ) = True
Else
LabDiagnostic.T ext = "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(Sourc e 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="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>

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

<script language="VB" runat="server">
''''''''''''''' ''''''''''''''' '''
Sub Page_Load(Sourc e 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(obj Sender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.T ext = "IS VALID"
Session("Valid" ) = True
Else
LabDiagnostic.T ext = "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="LabDiagnost ic" runat="server" />
<hr>
<ASP:Label id="LabDiagnost ic2" runat="server" />
<hr>
<ASP:Label id="LabDiagnost ic3" runat="server" />
<center>
<table border="1">
<tr>
<td align="right">F irst Name</td>
<td><ASP:Textbo x id="FirstName" MaxLength="40" Columns="40"
runat="server" />
<ASP:RequiredFi eldValidator id="rfvFirstNam e" runat="server"
ControlToValida te="FirstName"
ErrorMessage="* You must enter a first name."
Display="dynami c">
*
</ASP:RequiredFie ldValidator>

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

</table>
</center>
<center>
<table><tr><t d>
<ASP:Validation Summary id="valSummary " runat="server"
HeaderText = "<b>The following errors were found:</b>"
DisplayMode="Li st" ShowSummary="Tr ue" ShowMessageBox= "True" />
<!-- normally I have ShowSummary="Fa lse" -->
</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 "OnServerCl ick" 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
1235
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 must do this exactly!)
1
1355
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 originally could reproduce the problem, but now can't, even as I add complexity back in. The basic scenario is a custom DataGrid (just make a class that derives from DataGrid) that has a LinkButton in a HeaderTemplate that has a CommandName="sort"...
2
3449
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 contains a datagrid and some buttons and textboxes. the button events fire correctly and execute their code. however, the datagrid events fire a postback, and execute Page_load in the usercontrol, but the Item_Command code is not executed. the code to...
4
1434
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 display the results in a DIV that has a tree. I've tested all the code up until I add in the code for adjusting the tree in the DIV and it all works fine without problems. I can perform the search and put the results in the body of the page and...
1
1807
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 that is broken out into two sections. One section is where a user enters the main infomation around a financial outlook record and the second section is dynamic content that generates a table of values based on the infromation entered in the first...
5
1898
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 page which creates an new Article or Retreive an existing one depending if there is an ArticleID passed to it. So on my index page the user will be directed to the article page and will either pick up an existing one or have a new one created....
0
1051
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 Required Field Validators for both the drop down menus. I dont know whats the reason but everytime I change the dropdown menu the page takes a long time to reload and moreover I can even see the whole background graphic layout with no table...
7
4626
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, the client-side validation is executed immediately and the errors flash up on screen. However at the same time, a post-back is performed and the page is obviously un-usable while it is awaiting a response from the server. it looks very poor from...
0
1362
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 it's working fine(even from my laptop it's working fine) but when i called it from the any of the client LAPTOP (HP make) having Window XP Prfession ver 2002 i am get the main page properly,but when i click on any of the image button/link button (on...
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.