简体   繁体   中英

What is the best way to distinguish beteen “Refresh Post” or a “Real Post Back”

What is the best way to distinguish beteen "Refresh Post" or a "Real Post Back".

This is what I need to attain

protected void Button1_Click(object sender, EventArgs e)
{

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

}

I usually do a Response.Redirect to the same page in the postback event. That way all my Page.IsPostBack are real Postbacks and not Refreshes

You could set a hidden input with a nonce value generated randomly every time the form is loaded (but not on postback), then check if the nonce value got sent twice. If it got sent a second time, it was a refresh.

you could try like

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
}

Sample working code for the accepted answer

Add this line in designer

  <input type="hidden" runat="server" id="Tics1" value="GGG" />

Add following lined in the code behind

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM