Prevent duplicate postback in ASP.Net (C#)

若如初见. 提交于 2019-12-04 04:56:48

I dont think that you should be loading the server for trivial tasks like these. You could try some thing jquery UI blocking solution like this one. Microsoft Ajax toolkit should also have some control which does the same. I had used it a long time ago, cant seem to recall the control name though.

With jQuery you can make use of the one event.

Another interesting read is this: Build Your ASP.NET Pages on a Richer Bedrock.

Set a session variable when the user enters the page like Session["FormXYZSubmitted"]=false. When the form is submitted check that variable like

if((bool) Session["FormXYZSubmitted"] == false) {
   // save to db
   Session["FormXYZSubmitted"] = true;
}

I have had the same scenario. The solution of one of my coworkers was to implement a kind of Timer in Javascript, to avoid considering the second click as a click.

Hope that helps,

Disable the button on click, utilize jquery or microsoft ajax toolkit.

Depending on how important this is to you, could create an array of one time GUID's which you remove from the array once the update has been processed (ie posted back in viewstate/hidden field)

If the guid is not in the array on postback, the request is invalid.

Substitute database table for array in a clustered environment.

Client side can be tricky if you are using Asp.Net validation.

If you have a master page, put this in the master page:

 void IterateThroughControls(Control parent)
    {
        foreach (Control SelectedButton in parent.Controls)
        {
            if (SelectedButton is Button)
            {
                ((Button)SelectedButton).Attributes.Add("onclick", " this.disabled = true; " + Page.ClientScript.GetPostBackEventReference(((Button)SelectedButton), null) + ";");
            }

            if (SelectedButton.Controls.Count > 0)
            {
                IterateThroughControls(SelectedButton);
            }
        }
    }

Then add this to the master page Page_Load:

IterateThroughControls(this);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!