How to prevent users from repeatedly submitted data

xiaoxiao2021-03-06  108

This article looks on the grammatical, and the original author should be translated from us.

Simple solution: The easiest way is to use the response.redirect ("SelfPage" statement in your server-side control after the user is submitted. But most of the numbers include this method.

Submit multiple times: Please note: This article is not related to how to prevent multiple submission in a page. This article teaches you how to prevent the page from being refreshed after submitting the request. This is the case, and after the user submits the button, the end user cannot click the submit button. But so that end users can still submit data by clicking on the browser's refresh button. If you want to prevent multiple submission you can go to http://metabuilders.com/ to find some information on the Internet, there is a submission control available.

The solution to the conventional solution habit is to store the session ID and the sessionID stored in the ViewState to prevent the user from refresh screen screen. Prerequisite Your program allows automatic return, if not, you have to store this variable in Hidden Field. A typical example is given below. In the Page_Load event, you store the sessionID and a timestamp of the first submission. Protected system.Web.ui.WebControls.button submitbutton;

Protected system.Web.ui.WebControls.Label Refreshid;

Private Void Page_Load (Object Sender, System.EventArgs E)

{If (refreshid.text.length == 0) {refreshid.text = session.sessionID datetime.now.ticks.tostring ();}}

Private void Button1_Click (Object Sender, System.Eventargs E)

{String sesToken = (string) Session [FrameworkConst.SYNC_CONTROL_KEYWORD]; string pageToken = RefreshID.Text; if (sesToken = null && sesToken = pageToken!!) {Response.Write ( "The Refresh was performed after submit.");} else {// do your processing here to avoid Refresh trap Response.Write ( "The processing is done here Disabling submit button so that user can not perform multiple submit.."); Response.Write ( "But still user can peform Refresh on ");

Session [frameworkconst.sync_control_keyword] = session.SessionId DateTime.now.ticks.tostring (); refreshid.text = sestoken; submitbutton.enabled = false;

}

Different Solutions: Fortunately, ASP.NET provides some simpler methods. The disadvantage of the above solution is that we have to decide some logical issues in the event of the buttons. Imagine if there is a hundreds of lists to be submitted in your solution, you have to write a lot of such logic. Custom WEB controls and HTTP Modules provide the same solution. You can put this control on the page you need to control, it can work. Of course, not all the cases are needed, for example, the search page is allowed to refresh. However, the control refresh button is absolutely necessary when the page is inserted, updated, and deletes the database. Let's take a look at how the above scheme works. The first step: You need to register the HTTP Module module in the System.Web section.

Step 2: To put our development of the control on the page of the control page.

Working principle: It works in the foregoing normal speaking. Just provide a general method here. It provides a general way. In my opinion, if you have some good patterns, it will greatly speed up your development speed.

We need to store tags in the session during the first submission and compare them different when requested. With HTTP Handler, we store tags in Session. There is such an event prerequesthandlerexecute we can find the session through it, if it is other event, Session does not exist, such as the BeginRequest event. The value of both is compared in this event, if different, prove is a refresh event. At this time, you can add your own processing method, I usually tell the user to a page to tell the user to submit repeatedly. Private Void OnPrerequestHandleRexecute (Object Source, Eventargs E)

{

HttpContext context = (httpApplication) source .context;

String_keyword = frameworkconst.sync_control_keyword;

String sestoken = (string) context.session [_keyword];

String reqToken = context.request.params [_keyword]; // Save session and tag values ​​if not submitted

if (reqToken = FrameworkConst.BYPASS_SYNC_KEYWORD!) {context.Session [_keyword] = context.Session.SessionID DateTime.Now.Ticks.ToString ();} if (!! reqToken = null && reqToken = sesToken) {string path = CONTEXT.REQUEST.ApplicationPath & "/ Common / SyncControl.aspx? ReturnURL =" & context.request.url.absolutePath; context.server.transfer (pat);}}

The SyncControl control will create a Hidden Input Field will save the session set in HTTP Module.

转载请注明原文地址:https://www.9cbs.com/read-103050.html

New Post(0)