Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompted for Confirm Form Resubmission on refreshes after postback. What am I doing incorrectly?

I have a dashboard which starts in a blank/default state. I am giving the user the ability to load a saved state into the dashboard. When they click the 'Apply' button I run the following code:

function CloseAndSave() {
    var radUpload = $find(radUpload1ID);
    var inputs = radUpload.getFileInputs();

    if (inputs[0].value.length == 0) {
        alert('Please select a dashboard to upload.');
        return;
    }

    if( !radUpload.isExtensionValid(inputs[0].value) ) {
        alert('Please select an XML file.');
        radUpload.clearFileInputAt(0);
        return;
    }

    oWindow = null;
    __doPostBack(radButton1ID);
}

protected void RadButton1_Click(object sender, EventArgs e)
{
    if (RadUpload1.UploadedFiles.Count > 0)
    {
        UploadedFile dashboardXMLFile = RadUpload1.UploadedFiles[0];

        SerializableDictionary<string, string> dataToLoad = new SerializableDictionary<string, string>();
        XmlSerializer xmlSerializer = new XmlSerializer(dataToLoad.GetType());

        using (StreamReader reader = new StreamReader(dashboardXMLFile.InputStream))
        {
            dataToLoad = (SerializableDictionary<string, string>)xmlSerializer.Deserialize(reader);
        }

        foreach (var entry in dataToLoad)
        {
            string sessionKey = entry.Key;

            if (!string.IsNullOrEmpty(entry.Value))
            {
                Type type = StateManager.GetTypeFromStateName(sessionKey);

                byte[] data = Convert.FromBase64String(entry.Value);
                using (MemoryStream memoryStream = new MemoryStream(data))
                {
                    xmlSerializer = new XmlSerializer(type);
                    SessionRepository.Instance.SetSession(sessionKey, xmlSerializer.Deserialize(memoryStream));
                }
            }
        }
    }
}

RadButton1 has the property "AutoPostBack" set to false. I have set AutoPostBack to false because I wanted to perform validation before running the click event. So, now, I perform client-side validation and then allow the button click to run.

There's no update panel wrapping RadButton1. As such, the whole page posts after RadButton1_Click. This causes the state of the page to 'load up' the parsed state.

At this point, if I refresh the page, Google Chrome says "Please confirm form resubmission." I've read about how to squelch this, but I'd rather track down root cause.

Solution:

//This changes the response to a GET instead of a POST. Prevents the 'Form Resubmission' dialog.
Page.Response.Redirect(Page.Request.Url.ToString(), true);
like image 342
Sean Anderson Avatar asked Sep 08 '11 18:09

Sean Anderson


1 Answers

When you refresh the browser, it will resend the last request you did. If it was a POST request (like you do in case of postback) then it will re-post the information but before doing it you'll see the warning message you describe.

The best way to avoid this problem is implementing the pattern Post/Redirect/Get

Post/Redirect/Get (PRG) is a common design pattern for web developers to help avoid certain duplicate form submissions and allow user agents to behave more intuitively with bookmarks and the refresh button.

Normally people don't implement this (although we should) unless the re-post may cause some data inconsistency.

like image 152
Claudio Redi Avatar answered Sep 29 '22 05:09

Claudio Redi