ASP. NET background code recognition page "click button" and browser F5 "refresh" Events

Source: Internet
Author: User

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:

    1. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.