区分“刷新帖子”或“真实帖子后”的最佳方法是什么

时间:2011-02-10 09:50:26

标签: c# .net asp.net postback refresh

区分“刷新帖子”或“真实邮寄”的最佳方式是什么。

这是我需要获得的

protected void Button1_Click(object sender, EventArgs e)
{

if(PostBack && !Refresh)
{
//Do Something
}

}

4 个答案:

答案 0 :(得分:3)

我通常在回发事件中执行Response.Redirect到同一页面。 这样我的所有Page.IsPostBack都是真实的回发而不是刷新

答案 1 :(得分:1)

您可以设置一个隐藏的输入,每次加载表单时都会随机生成一个nonce值(但不会在回发时),然后检查nonce值是否被发送了两次。如果第二次发送,则刷新。

答案 2 :(得分:1)

你可以试试

protected void Button1_Click(object sender, EventArgs e)
{
    //your code of Click event
    //..............
    //...............
    // and then add this statement at the end
    Response.Redirect(Request.RawUrl); // Can you test and let me know your findings
}

答案 3 :(得分:1)

接受答案的示例工作代码

在设计师

中添加此行
  <input type="hidden" runat="server" id="Tics1" value="GGG" />

背后的代码中添加以下内容
 public partial class WebForm1 : System.Web.UI.Page
{

    long tics = DateTime.Now.Ticks;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.Tics1.Value = tics.ToString();
            Session["Tics"] = tics;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["Tics"] != null && Request["Tics1"] != null)
        {
            if (Session["Tics"].ToString().Equals((Request["Tics1"].ToString())))
            {
                Response.Write("Postback");
            }
            else
            {
                Response.Write("Refresh");
            }
        }
        this.Tics1.Value = tics.ToString();
        Session["Tics"] = tics;        
    }
}
相关问题