代码之家  ›  专栏  ›  技术社区  ›  eKek0

阻止浏览器在刷新时显示“查询重新提交”对话框

  •  3
  • eKek0  · 技术社区  · 16 年前

    当用户从下拉列表中选择一个项目并按下按钮时,我的应用程序将显示一个根据所选值手动绑定和过滤的数据列表。如果用户按下浏览器的刷新按钮,它会要求确认用户是否确实要再次提交查询。

    我不希望浏览器问这个问题。我怎样才能避免这种行为?

    据我所知,这可以通过实现post/redirect/get模式来实现,但我不知道如何在ASP.NET3.5中实现。

    2 回复  |  直到 16 年前
        1
  •  1
  •   BC.    16 年前

    PRG模式对asp.net的意义是,测试回发、执行处理并将用户重定向到不同的页面(或使用不同查询字符串的同一页面以更改该页面的行为)。

    此模式的问题是,您将丢失asp.net的所有回发功能,如viewstate和自动表单处理。

        2
  •  0
  •   Zhaph - Ben Duguid    16 年前

    是的,在页面加载事件中,类似这样的操作将为您完成任务,例如:

    // Check to see if the user submitted the form:
    if (Page.IsPostBack){
      // Get the Params collection - query and forms
      NameValueCollection params = Request.Params;
      StringBuilder query = new StringBuilder();
    
      // Iterate over the params collection to build a complete
      // query string - you can skip this and just build it
      // manually if you don't have many elements to worry about
      for(int i = 0; i <= params.Count - 1; i++)
      {
        // If we're not on the first parameter, add an & seperator
        if (i > 0){
          query.Append("&");
        }
    
        // Start the query string 
        query.AppendFormat("{0}=", params.GetKey(i));
    
        // Create a string array that contains
        // the values associated with each key,
        // join them together with commas.
        query.Append(String.Join(",", pColl.GetValues(i));
      }
    
      Response.Redirect(String.Format("{0}?{1}", 
                          Request.Url.AbsolutePath, query.ToString()))
    }
    

    此模式的另一个问题是,您将在历史记录中使用此附加重定向,这可能会导致用户必须单击“上一步”两次才能返回到您的搜索表单。

    好的一面是,他们现在可以很高兴地将结果页面添加到书签中,并且无需重新提交表单即可返回结果。