ASP.net: fixing double postback with Firefox

Sometimes an ASP.net page postbacks twice when executed in Firefox: how to solve?


How to fix double postback with Firefox

A couple of days ago I faced with a strange problem with an ASP.net page: clicking on a button, the page were postback-ed twice when executed on Firefox; by investigating more, I discovered that the second one wasn’t really a postback, but a full reload (hence, Page.IsPostBack was returning False on the second postback).

Googling around, I’ve found that Firefox sometimes behaves this way, when an invalid or empty url reference is used somewhere in the page (don’t ask me why: I don’t know!).

Then I begin to inspect my code, and found a statement like this:


background-image:url('')

Now, if you can identify the code that produces the empty url reference, just correct (or remove) it, and the page postback will work as expected.

Unfortunately, in my case, the statement was generated by a third party control, so I had to intercept and modify the ouput before its rendering, by overrinding the Page Render method; this is my way:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

Dim sb As New System.Text.StringBuilder

Dim sw As New System.IO.StringWriter(sb)

Dim htw As New System.Web.UI.HtmlTextWriter(sw)

MyBase.Render(htw)
htw.Flush()

' removes the empty url declaration that causes a double postback on FireFox
sb.Replace("background-image:url('')", "")

writer.Write(sb)

End Sub

Of course, you can add more tags to be intercepted and removed depending on your needs.

Replica

%d blogger hanno fatto clic su Mi Piace per questo: