Code in Page_Load executes twice

D

David C

I spent the last four hours trying to figure out why Page_Load would
execute twice. Even stranger was that everything within if (!
IsPostBack){....} executed twice as well. There is no rhyme or reason
for that.

Here is what I found out.

When you have an ImageButton with no ImageURL set, it will always do
that. This was not the case with .NET 1.0. So if you need to
experiment with ImageButton, set something to the ImageUrl
attribute.

This has to be a bug, but who knows.

3:55 AM. Good night and Death to Microsoft.
 
J

Joey

David,
No its not a bug. What might be happening is that you've got
AutoEventiWireup="true" in your <%@ Page directive, PLUS you may also have
duplicate event delegate wireups in your codebehind. One or the other needs
to go.
There may be other causes to this problem also, but usually that's the main
one.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com









- Show quoted text -
duplicate event delegate wireups in your codebehind.

what do you mean by this? can you give an example?

We have always had problems with Page_Load and !IsPostBack firing
multiple times. I would LOVE to fix this.
 
D

David C

David,
No its not a bug. What might be happening is that you've got
AutoEventiWireup="true" in your <%@ Page directive, PLUS you may also have
duplicate event delegate wireups in your codebehind. One or the other needs
to go.
There may be other causes to this problem also, but usually that's the main
one.

Peter, it is eminently reproducible. Have you tried it? Start a new
website, on Default.aspx, throw in an image button control without
setting ImageUrl and put your break point in Page_Load. If it does
not, I would like to know.

And then put something for ImageUrl. It does not have to be a
existent file. Put anything and then run it. It does not execute
Page_Load twice.


This is what my aspx page looks which executes Page_Load twice.

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="blah.jpg"/>
</div>
</form>
</body>
</html>
 
G

Guest

for example:
Page directive:
<%@ PageAutoEventWireup="true" ...
and in Page_Init:
this.Load += new EventHandler(Page_Load);

I think the default for the page is true, unless you specifically override
this either in web.config or on the page level
 
J

Joey

this.Load += new EventHandler(Page_Load);

I don't have any lines of code that do this. I always select the
control in the properties editor in the IDE, click the show events
button (lightning bolt) and then double click in the appropriate
field. An example would be RowDataBound for a gridview. Then I am
taken back to my code behind page to a stubbed out code block. At this
point I know the wireup for the event has been created...it's just not
in MY code.

Now, we have LOTS of pages that execute Page_Load and !IsPostBack code
blocks two, three, four or more times. In some situations this creates
serious problems, such as database inserts occurring twice, etc...This
has been a thorn in my side for a long time, and I would really like
to figure out what is causing it.

Oh yeah, we have lots of imagebuttons without imageurl properties set
to anything. They are skinned instead.

We have also noticed this to be much more of a problem when using
Firefox.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top