Recently, some friends asked me about page refresh and buttons. Simply put, when a button event occurs, the user refresh the page by pressing F5 or refresh, in this way, the event in the button will be executed repeatedly, which is not what we want to see.
In the past, there were some solutions, some of which were completed by JavaScript, and some were completed by the background.CodeCompleted. The former is not the focus of today's discussion. Today, we will introduce you to a "refresh" Monitoring Method completed by C #. When you use "F5" or the "refresh" button of the browser, the code will detect it and return it as a bool value. This is separated from the previous class library of the boss, and the name of the boss is forgotten.... It removes irrelevant content and only keeps the detection and refresh modules, which is clean.
What is "refresh?
Refresh is completed by repeatedly submitting the latest request to the server through the browser. When a user clicks a button, the browser caches the button status. If the user then uses the refresh function, the browser will mechanically submit the "latest request" containing the number of Button states, and the browser will not provide any notification for the "refresh" event (in fact, the browser only provides notifications for "go to" events). Therefore, the server cannot distinguish between refresh and general submission events. In this way, the server will naturally execute button events again after receiving the request.
Identify the "refresh" principle:
Use a ticket ID that is unique in the context. The ticket ID increments with the page submission or sending back. When the user interacts with the server, the Code extracts the last ticket number and compares it with the current ticket number. If the current ticket is larger than the previous one, it is submitted or resent; otherwise, it is recognized as refreshing.
In this method, httpmoudel is used and the session is operated.
Usage:
- Introduce DLL
Http://files.cnblogs.com/isline/MsdnExt.rar
2. modify web. config and add the following items in
<Add name = "msdnmodule" type = "msdn. refreshmodule, msdnext"/>
"Msdnmodule" is a name, "msdn. refreshmodule" is the "refreshmodule" type under "msdn", and "msdnext" isProgramSet Name
3. Introduce the namespace using msdn and modify the page class to inherit the msdn. Page
4. Use Code
Code
Protected Void Button#click ( Object Sender, eventargs E)
{
If ( ! Ispagerefresh)
Response. Write ( "Button event " );
Else
Response. Write ( "Page refresh " );
Trackrefreshstate ();
}
Source codeDownload:
Http://files.cnblogs.com/isline/Refresh.rar
Note:
It is best to use this method in the method corresponding to the button event, otherwise it may become invalid.
I amAicken)Welcome to my next articleArticle.