Page_load event fires twice with FireFox only

I came across weird behavior of Firefox 3.0.X that submits forms twice.  Let me explain what I have seen.

  • Main.ascx has a reference of UC1.ascx
  • Main.ascx also has a UC2.ascx control reference.

When I make the request to PageX.aspx that contains Join.ascx control.  Here is the page execution cycle I am seeing.

  • Page_Load (Main.ascx) Page.IsPostBack is false
  • Page_Load (UC1.ascx) Page.IsPostBack is false
  • Page_Load (UC2.ascx) Page.IsPostBack is false
  • Page_Load (Main.ascx) Page.IsPostBack is false
  • Page_Load (UC1.ascx) Page.IsPostBack is false
  • Page_Load (UC2.ascx) Page.IsPostBack is false

When I click on some control like button that triggers the page post back, I am seeing

  • Page_Load (Main.ascx) Page.IsPostBack is true
  • Page_Load (UC1.ascx) Page.IsPostBack is true
  • Page_Load (UC2.ascx) Page.IsPostBack is true
  • Page_Load (Main.ascx)  Page.IsPostBack is false  (this one creates a problem. In my case code clears some session value if page postback is false)
  • Page_Load (UC1.ascx)     Page.IsPostBack is false
  • Page_Load (UC2.ascx)    Page.IsPostBack is false

You may have noticed that Page_Load executed twice.  It’s really bad things from performance point of view and also for web analysis tool that tracks page hit (may be a good thing if you have custom code that increments counter in Page_Load so you will have page hit 2x(double page hit value) but actual value is x if user uses firefox 3.0.x).

But what causes the Firefox to execute the page request twice?  I am aware of following two things.

  • If you have img control with empty string assigned to src attribute.
    • <img src=””/>
    • <asp:image runat=”server”  id=”img”/>

You may be forgot to assign imageurl or wanted to assign imageurl in code behind based on the some condition and that condition never gets executed and ended up being empty string assigned to src attribute when ASP.Net renders the page.

  • If you have empty string assigned to href attribute to html link for stylsheet.
    • <link rel=”stylesheet” href=””/>
  • If you have empty src attribute set to script.
    • <script src=”” type=”text/javascript”>

Worst thing is that if you have both img and link into page, page gets executed three times(first normal, second for empty src attribute and third for empty href attribute).

The reason why Firefox is doing that when It finds empty src attribute it makes additional request to the same page to resolve the URI as per standard behavior defined in RFC 3986 – Uniform Resource Identifiers section 5.2 section 5.4.  So Firefox isn’t bad it follows the standard but that creates the problem for us(specially for me who is in Flex team and assigned weird bug that only happening in Firefox and took my almost 1.5 days to figure this out L).

Nice thing is that they have fixed this behavior in new version of Firefox 3.6.X.

CAUTION: Don’t leave SRC attribute empty. If you want to assign imageURL in code behind based on some condition, you can still assign very tiny pixel image(that you barely see) in aspx page so that we have at least image assigned until your website support Firefox 3.0.x.

Posted in Microsoft Technology Tagged with: ,
2 comments on “Page_load event fires twice with FireFox only
  1. Safi says:

    you save my time

    thanks

  2. Morpheus says:

    Oh, thanks a lot. This article saves my time.

Ads