0

So I have my post method like such:

<form method = "post">

And in the post, I have this:

<input name="insertInto" type="submit" value="Insert">

To "catch" that the Insert button has been clicked, I used this in my VB.NET code:

If (request.Form("insertInto") = "Insert")
End If

Now this works near perfectly...except when I refresh the page. When I refresh the page, the post seems to "post" again. As in, the Insert button seemingly gets hit again just by me simply refreshing the page. How do I prevent this?

Leon
  • 571
  • 2
  • 9
  • 24
  • 1
    Take a look on the following thread http://stackoverflow.com/questions/12353461/how-to-prevent-data-from-sending-if-user-press-f5-or-refresh-in-asp-net – Damian Kobak Apr 08 '15 at 13:47

2 Answers2

0

When you hit "refresh", the post data is sent again to the page (an alert box is displayed in modern browsers informing you that).

One way to avoid that is to have a page between the displaying html and the action execution. The form then, is submitted to the action execution and, after whatever action is done, it forwards the user back to the HTML page.

Does this makes sense?

Paulo Henrique Martins
  • 1,440
  • 1
  • 10
  • 7
  • Thanks for the tip, but this seems like a quick fix to me. Is this the only way to prevent the reposting? – Leon Apr 08 '15 at 12:55
0

In your answer, you mention you've used VB.Net code to help with the execution of your Insert code. You could further expand your usage of VB.Net in the code behind to help with the problem you are faced with.

Here is an example which hopefully helps you:

In your ASPX page, you could replace your form code with the following:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="btnInsertInto" Text="Insert" />
    </div>
    </form>
</body>
</html>

The submission of the form is now handled by the ASP Button (asp:Button) and is connected to an event in the code-behind file:

Private Sub btnInsertInto_Click(sender As Object, e As EventArgs) Handles btnInsertInto.Click
        ' Handle button click code here..
End Sub

Within the same code-behind class, you can make use of the Page_Load sub to handle your page initialization code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
            ' Page initialization code goes here..
    End If
End Sub

So, the entire code-behind class looks like:

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ' Page initialization code goes here..
        End If
    End Sub

    Private Sub btnInsertInto_Click(sender As Object, e As EventArgs) Handles btnInsertInto.Click
        ' Handle button click code here..
    End Sub
End Class

When I tested this before posting, I was able to refresh the page without the insert code being executed.