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

*爱你&永不变心* 提交于 2019-12-12 09:39:39

问题


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);

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7352928/prompted-for-confirm-form-resubmission-on-refreshes-after-postback-what-am-i-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!