Page Load Event Fires Twice

G

Guest

I am having a problem that the page load event fires twice in reponse to an
autopostback. The first time, the IsPostBack property is true, and the
second time it it false. This is causing many uneeded database calls, since
many of them do not need to be fired on a PostBack.

I have already set the AutoEventWireup=false in the ASPX @Page directive. I
am setting the visible property of a datagrid in some of my methods, but I
commented out that line completely and I still have the problem.

Any help is appreciated.

Thanks,
Asa Monsey
 
P

Peter Rilling

This often happens when you have a handler registered more then once.

See if you have the event registered in both the HTML and C# code.
 
S

Steven Cheng[MSFT]

Hi Asa,

From the description, one of your asp.net's Page_load is entered twice and
the first time the
"IsPostBack" property is true and the next time, it is "false" ,yes?

As for the first "enter"(IsPostBack == true), I'm also abit confused. Is
there any particular code in the codebehind or any html element in the aspx
page template which may cause the page be self requested? Also, to
throubleshoot, you can build a simple page which can repro the problem and
analyize the page's html source step by step
In addition, you can also provide the problem page's code here so that we
can do some tests locally. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

Hi,

Thanks for the followup and the code snippet you provided. I copy the page
code into one of my web project and in my test, the page only load once.
How do you found that it is loaded twice? Via set break point and F5
debugging or put some code in the Page_Load event?
I think you can put some code such as
Response.Write (..) to write some statements to see whether it is always
output twice (both initial request and postback)

Also, I suggest that you create another new web application and put the
simple page in it to see whether the problem reoccur. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

Hi Asa,

Thanks for your followup. I think you've excatly found the cause and after
some futher test, here are my understanding on the reason of this behavior:

1. First the <body> 's "background" attribute should be set with a url
value since its for settintg the page's background image rather than
background color :)

2. Then, when you set the "background" as a web color value such as
#XXXXXX, the web browser will treat the "#XXXXXX" as a url. As we know, the
#XXXXXX is are particular link type( anchor) which point to the self page's
certain portion. That makes the browser send another get request when
parsing to the <body background="#xxxxxx" > line.
Also, you can make a same test via the following page code:

<body>
<img src="#" />
.....
</body>

This will also cause the self page be requested twice. But this only occurs
when the page is posted back rather than the inital request, the reason is
in 3.

3. The IE browser will treat the GET and POST request to a single url as
different request. When the page is first time requested( not post back),
it send a get request, and the page return, and when parsing to the <body
background="#xxxxxx" >, it won't send a new GET request because it has done
it already.
However, when post back, since post back reqeust is a POST request, IE
browser think it is different from the GET request, so he will send another
GET request when dealing with the <body background="#xxxxxx">

Anyway, I'm glad that we finally figure out the problem. And it maybe a
good instruction when next time we meet such problem. :)

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
G

Guest

Steven,

Thank you for your thorough explanation of the causes of this behavior. It
makes perfect sense now. I don't know why the color reference was in that
attribute, since I have inherited this code from previous developers.

This should significantly improve our database load, since there will be
many fewer calls now.

Thanks again,
Asa Monsey
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top