在回发后刷新时提示确认重新提交表单.我做错了什么? [英] Prompted for Confirm Form Resubmission on refreshes after postback. What am I doing incorrectly?

查看:992
本文介绍了在回发后刷新时提示确认重新提交表单.我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以空白/默认状态启动的仪表板.我为用户提供了将保存的状态加载到仪表板的功能.当他们单击应用"按钮时,我运行以下代码:

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的属性"AutoPostBack"设置为false.我将AutoPostBack设置为false,因为我想在运行click事件之前执行验证.因此,现在,我执行客户端验证,然后允许单击按钮运行.

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.

没有用于包装RadButton1的更新面板.这样,整个页面将在RadButton1_Click之后发布.这将导致页面状态加载"已解析的状态.

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.

这时,如果我刷新页面,则Chrome浏览器会显示请确认重新提交表单".我已经读过有关如何抑制这一问题的信息,但是我宁愿追查根本原因.

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.

解决方案:

//This changes the response to a GET instead of a POST. Prevents the 'Form Resubmission' dialog.
Page.Response.Redirect(Page.Request.Url.ToString(), true);

推荐答案

刷新浏览器时,它将重新发送您的上一个请求.如果这是一个POST请求(如在回发的情况下一样),它将重新发布信息,但在执行此操作之前,您会看到描述的警告消息.

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

发布/重定向/获取(PRG)是Web开发人员常用的设计模式,可帮助避免某些重复的表单提交,并允许用户代理使用书签和刷新按钮来更直观地表现.

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.

这篇关于在回发后刷新时提示确认重新提交表单.我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆