Search code examples
vb.netvisual-studio-2005refreshbuttonclick

Prevent button serverclick firing on every page refresh


Anyone know of a way to prevent a button serverClick event from firing on page refresh?

Markup:

<INPUT id="btnGo" type="button" value="Go" runat="server">

VB.net:

Public Sub btnGo_ServerClick(ByVal sender As Object, _
 ByVal e As System.EventArgs) Handles btnGo.ServerClick

The btnGo_ServerClick function needs to fire on the button click, but not on page refresh.

I've tried using IsPostback, but that is always true- even on the button click.

N.B. I'd rather not go into the reasons behind why I'm using a serverclick, rather than an asp:button and a click event. Suffice to say it just isn't suitable for I am doing.


Solution

  • You'll have to implement something like this: http://www.csharpaspnetarticles.com/2008/12/detect-browser-refresh-to-avoid-events.html

    I'm not sure what you're doing in the server click event, but it's probably a better idea to add form validation or a redirect after the form has been submitted.