By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

Page Load Event Fires Twice

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

"Asa Monsey" <As*@NoMoreViri.com> wrote in message
news:91**********************************@microsof t.com...
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

Nov 18 '05 #2

P: n/a
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

Nov 18 '05 #3

P: n/a
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

Nov 18 '05 #4

P: n/a
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

Nov 18 '05 #5

P: n/a
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

"Steven Cheng[MSFT]" wrote:
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


Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.