Prevent form submission to ASP.Net (without redirecting to itself)
I have a main page with a form element ( <form runat="server">), a content page with a Button ( <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Sync" />) element, and a Code Behind page containing a function Button1_Click.
The user comes to the page and presses the button. Code Behind the code is executed (on the server) in which it updates some tables in the database. The last thing code does code is to install a InnerHTMLSpan element on the content page with a success or failure message.
Everything works great. The problem is that the user refreshes the page, the form is resubmitted, and the browser asks if this is really what the user wants. If the user answers in the affirmative, the Code after code is re-executed and the database is updated again. If the user responds negatively, then nothing happens.
Code re-execution Behind the code is not a big problem. Nothing will hurt. But this is really not the behavior I want.
I know that I can redirect back to the page with Response.Redirect (), but then the user will never see my success or error message. I should mention that the message is really more than just “Success” or “Failure”, otherwise I suppose I could add something to the QueryString to redirect.
Is there a way to reset the Form element from Code by Code so that if the user refreshes the page, the form will not be resubmitted?
Main page ...
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="IntuitSync.SiteMaster" %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:ipp="">
<head runat="server">
</head>
<body>
<form runat="server">
<div runat="server" id="mainContetntDiv">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</form>
</body>
</html>
Content...
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.master" CodeBehind="SyncToCloud.aspx.cs" Inherits="IntuitSync.SyncToCloud" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Sync" />
<span runat="server" id="SyncStatus"></span>
</asp:Content>
Code for ...
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Web;
namespace IntuitSync
{
public partial class SyncToCloud : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//
}
protected void Button1_Click(object sender, EventArgs e)
{
/*
Do a bunch of stuff and put the results in syncResults which is a List.
*/
SyncStatus.InnerHtml = string.Join("", syncResults.ToArray()); // I'd rather do this...
//Response.Redirect(Request.Url.PathAndQuery, true); // ...and not do this.
}
}
}
Any help is appreciated.
The problem is that you are executing a POST form, which, if the user subsequently refreshes the page, does what it should do: resubmit the POST. There is no such thing. Therefore, the following options are available:
- Redirect to the confirmation page, transferring the results of the operation to the confirmation page in some way (as a rule, reload from some data repository if this is not possible for the request line / cookie / etc.).
- , ( /ViewState/what -have-you-siliness).
- , , , - , - . . , / get.
, , , " syncResults..." .
JASON
SyncToCloud.aspx.cs
[System.Web.Services.WebMethod]
public static string updateSyncStatus()
{
/*
Do a bunch of stuff and put the results in syncResults which is a List.
*/
SyncStatus.InnerHtml = string.Join("", syncResults.ToArray()); // I'd rather do this...
//Response.Redirect(Request.Url.PathAndQuery, true); // ...and not do this.
}
SyncToCloud.aspx
function SyncStatus(){
$.ajax({
type: "POST",
async:true,
url: "syncToCloud.aspx/updateSyncStatus",
data: <if have anydata>,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d == "Success") {
//Set message on some lable or do something
} else {
//Proceed to show message on wrong password
}
}
});
}
buutton
<asp:Button ID="Button1" runat="server" OnClientClick="SyncStatus(); return false;" Text="Sync" />