How to prevent repeated postbacks from confusing m

2019-01-19 20:05发布

I have a web application (ASP.Net 3.5) with a conventional 3 layer design. If the user clicks a button a postback happens, some middle and data layer code runs, and the screen is refreshed. If the user clicks the button multiple times before the first postback is completed my logic gets confused and the app can end up in an invalid state. What are the best ways to prevent this?

I can use javascript to disable the button but this just hides the problem. How do I build my business and data layers to handle this?

7条回答
戒情不戒烟
2楼-- · 2019-01-19 21:09

We have all seen the websites that disable "submit" buttons when you click on them. This is often done to prevent users from clicking the button multiple times. Normally this is accomplished using an 'onclick' JavaScript event to disable the button. In ASP.NET, each server side item already has a onclick event handler which calls the server back for event processing. To accomplish the same thing in ASP.NET, you could easily do:

btnSubmit.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnSubmit).ToString());

Where 'btnSubmit' is the name of the button in question. What happens here is we create an onclick event that does two things. Firstly, it disables the button in the users browser. The second thing it does is submit the normal postback event to the server.

查看更多
登录 后发表回答