Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid the button events on Refresh of page

I have .aspx page that page inserts the data to the database on a button click. But when i press the button it is going right. i m getting the Successfully message as " successfully inserted data". In this situation if i press "F5" or Refresh the page it is firing the button click event. Why it should be ? How to avoid this condition ?

like image 817
Red Swan Avatar asked Jan 30 '10 10:01

Red Swan


People also ask

How do you prevent button click event firing when a page is refreshed?

One way to prevent this from happening is to use Response. Redirect("same_page") to the same page after the event logic. This will force the page to reload and thereafter doing any further page refreshes would not call the button click event.

How do I stop the button from refreshing the page react?

Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.

Why does my button refresh the page?

Your page is reloading because the button is submitting your form. The submit will, by default, re-load the page to show form errors or the result of the submit action. The cleanest fix is, as you discovered, to specify a button type.

How do I refresh a page that clicks a button?

Method 1: Using the location.reload() method reloads the current web page emulating the clicking of the refresh button on the browser. The optional true parameter passed to the method is used to force the page to load from the server and ignore the browser cache.


2 Answers

When the user clicks F5 (or uses a toolbar button to refresh the page) it will cause a new request, identical to the previous one, to be sent to the server. The Button.Click event will be raised again, but you have a few ways to protect yourself against inserting the data twice.

The best way, IMHO, is to use the Post/Redirect/Get pattern. In your code, right after the point where the data is saved, do a 302 redirect to a confirmation page:

protected void btnSaveStuff_Click(object sender, EventArgs e)
{
    SaveStuffToDatabase();
    Response.Redirect("confirmation.aspx");
}

When using the pattern, the POST to the original page will not end up in the browser history, and refreshing the result page will cause the final GET to be repeated, which should be safe.

like image 149
Jørn Schou-Rode Avatar answered Oct 05 '22 21:10

Jørn Schou-Rode


Add this in your class:

#region Browser Refresh
private bool refreshState;
private bool isRefresh;

protected override void LoadViewState(object savedState)
{
    object[] AllStates = (object[])savedState;
    base.LoadViewState(AllStates[0]);
    refreshState = bool.Parse(AllStates[1].ToString());
    if (Session["ISREFRESH"] != null && Session["ISREFRESH"] != "")
        isRefresh = (refreshState == (bool)Session["ISREFRESH"]);
}

protected override object SaveViewState()
{
    Session["ISREFRESH"] = refreshState;
    object[] AllStates = new object[3];
    AllStates[0] = base.SaveViewState();
    AllStates[1] = !(refreshState);
    return AllStates;
}

#endregion 

And in your button click do this:

protected void Button1_Click(object sender, EventArgs e)
{
    if (isRefresh == false)
    {
        Insert Code here
like image 30
live-love Avatar answered Oct 05 '22 21:10

live-love