Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

Last post 07-31-2008 12:44 PM by NC01. 26 replies.

Sort Posts:

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    05-25-2006, 11:23 AM
    • All-Star
      45,854 point All-Star
    • SomeNewKid
    • Member since 08-10-2003, 12:16 AM
    • Western Australia
    • Posts 8,027

    smart1:
    Why on earth would this happen, and what is a work around?
    If we follow the steps you outlined in your first post, here is the resulting HTML:

        <!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><title>
            Untitled Page
        </title></head>
        <body>
            <form name="form1" method="post" action="Default.aspx" id="form1">
        <div>
        <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
            value="/wEPDwUKMTk3MzY5NzU2NmRkcyJx3K2nk5TStoXXq40WY7ygMOg=" />
        </div>
            <div>
                <img id="Image1" src="" style="border-width:0px;" /></div>
            </form>
        </body>
        </html>

    What is important to note is that the src attribute of the <img> tag is an empty string.  So, what image file does the browser request? Well, whenever a browser comes across an empty address for a file it must request, it substitutes the document's own address. (There is one exception that is not relevant here.)  This is why a postback will work even if there is no action parameter--the form will be posted back to the document's own address.

    So, let's consider what happens when there is a break-point in the Page_Load of default.aspx.

    The browser issues a request for default.aspx, and the break-point in the Page_Load is caught.  We expect this.  Then the rest of the page renders and is returned to the browser. 

    Now, the browser sees that there is an <img> tag, so it must request an image.  What does it request?  It requests an image at default.aspx.  So, the browser issues a second request for default.aspx, and the break-point in the Page_Load is caught again.

    So, it is not that the same break-point is being hit two times during a single request.  Rather, the application is receiving two separate requests for the same default.aspx.  (If you dove down into the details of each request as shown in the debugger, you could probably find proof that the requests are separate and different.  But frankly, I wouldn't know where to look.)

    As proof that this is what is occuring, you can remove the <img> tag, and instead use Flash <object> element that also contains an empty source address.  Or you can use a <script> element with an empty src attribute.  In each case, the browser will substitute default.aspx for the missing address, and again it will show that the Page_Load break-point is being hit twice.

    That then is why your break-point is being hit twice--it is receiving two separate requests for the same .aspx document.

    This also explains why, when you give the <asp:Image> control a non-empty ImageUrl property, the break-point will not be hit twice.  There is no longer a second request for an image located at default.aspx.

    This also explains why Terri and NC01 did not see two hits to Page_Load when they turned on tracing for the page.  The Page_Load is hit once for default.aspx, and they see the trace output.  But then there is a second request made for the empty <img> tag, which will not show up on the trace output.

    I hope this helps you.
     

    Alister
  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    05-25-2006, 11:29 AM
    • All-Star
      45,854 point All-Star
    • SomeNewKid
    • Member since 08-10-2003, 12:16 AM
    • Western Australia
    • Posts 8,027

    Oh, another point.

    Presumably, you do not always want that image to appear, and that is why you might sometimes leave the ImageUrl unspecified.  As we've seen, that has a nasty side-effect of forcing two requests to your .aspx page.

    So, if you only sometimes want that image to appear, then you should initially set its Visible property to false.

        <asp:Image ID="Advertisement" Visible="false" Runat="server" />

    Then, if the logic in your code determines that that image is required, you need to do the following:

        Advertisement.Visible = true;
        Advertisement.ImageUrl = "~/advertisements/ad1.gif";

    If the Visible property is left as false, the control will not output an <img> tag to the resulting HTML.  That will prevent the nasty side-effect that has affected you.
     

    Alister
  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    06-14-2006, 5:21 PM
    • Member
      15 point Member
    • theyenine
    • Member since 06-14-2006, 9:19 PM
    • Posts 6
    That oughta do it. Thanks.
  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    06-17-2006, 3:25 AM
    • Member
      17 point Member
    • AliC
    • Member since 04-07-2006, 12:29 PM
    • Posts 4

    that seems to make sense.

     

    thanks all

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    06-19-2006, 5:14 AM
    • Member
      5 point Member
    • shyammaddi
    • Member since 06-19-2006, 9:09 AM
    • Posts 1

    May be the problem in autoeventwireup="true" or "false"

     

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    06-21-2006, 11:23 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs
    shyammaddi:

    May be the problem in autoeventwireup="true" or "false"

    No it isn't. The problem in short is that he has no src value in his html/aspx file and he's setting it in the CodeBehind, which causes the page to re-load.

    NC...

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    03-04-2008, 3:02 PM
    • Member
      2 point Member
    • bgray
    • Member since 03-04-2008, 7:50 PM
    • Posts 2

    This is a known bug in the rendering of the img tag from the server control. You must set the imageURL property to some value other than null or String.Empty before the control renders. Please see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=330201

    Although the behavior of the rendered img tag without a valid source is intentional and known, the root cause is the lack of robustess in the server control.

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    03-04-2008, 4:44 PM
    • Member
      17 point Member
    • AliC
    • Member since 04-07-2006, 12:29 PM
    • Posts 4

    hey thanks bgray

     

    interesting....

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    04-01-2008, 9:59 AM
    • Star
      9,457 point Star
    • kamii47
    • Member since 05-26-2005, 4:04 PM
    • Karachi, Pakistan
    • Posts 2,184

    I expect this should be solved in .net 2.0 sp1 but it isn't

    Kamran Shahid
    Sr. Software Engineer
    (MCP,MCAD.net,MCSD.net,MCTS,MCPD.net[web])

    Remember to click "Mark as Answer" on the post that helps U
  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    04-01-2008, 11:41 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    I don't believe that has as much to do with the .NET Framework as the way an HTML img tag works.

    NC...

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    07-31-2008, 12:40 PM
    • Member
      2 point Member
    • xueco
    • Member since 07-31-2008, 4:25 PM
    • London
    • Posts 1

    I experienced the same problem and wasted 1 day to find out it was this.

    The CMS component was adding the string: <img src="" /> (not even an ASP.NET control) to the page because the user forgot to specify the image and because this the Page_Load twice!

    This problem was breaking the logic in the code-behind.

    Is there a easy way to avoid that? I could try to be less permissive in my CMS users but this will be at expense of making the CMS less flexible.

     

    Thanks

  • Re: Bug in ASP.NET? Adding an image control forces Page_Load event to fire twice

    07-31-2008, 12:44 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    Like I said earlier, I think that it is a problem with the way image tags are parsed in HTML. Try setting the src property to a dummy image if empty.

    NC...

     

Page 2 of 2 (27 items) < Previous 1 2