Asp.net button pitfall
I had just resolved a very ugly bug in one of our screens. The behavior was the following:
You open a the page and click on a “cancel” button that closes the current screen and refresh the parent screen.
So far all is sweet.
Now you do it again (open & click “cancel”)
So far all is sweet.
Try to open any other page.
gosh! nothing works! Everything got stuck somehow…
The cancel button was declared like this:
<asp:Button id=“…” runat=“server” OnClientClick=“RefreshParentAndCloseMe();” />
Can you spot the problematic approach here?
Button.AutoPostBack is set to True as default which means that clicking on the button triggered page submit but at the same
time we run javascript code to close the window. My guess is that the server gets a new request and handles it but the client
is dead until then(until the response is ready).
Playing with this behavior can lead to unexpected behavior (in our case – we had to close the browser and start over)
Solutions?
A. Use html button (I prefer this one as this pure client behavior button)
B. change the OnClientClick to: OnClientClick=”RefreshParentAn
C. Set AutoPostBack=False on the button.