Page extension class that solves the problem of repeated data submission caused by browser refreshing.

Source: Internet
Author: User

In order to solve the. NET repeated submission problem, the network also searched for a long time and found this page extension class. It still feels good.

Share it with you

For specific use, you only need to inherit the extension class from the page and make a judgment in the submitted method.

If (! This. isrefreshed ){

Submit the code for the first time.

}

Else {

You can report an error message or throw an exception when you submit multiple tasks !~

}

Here, isrefreshed is the bool attribute in the extended class. If it is set to false, it indicates that it is the first submission. Otherwise, it is a repeated submission and will not be processed.

 

The Code is as follows:

 

1 using system;
2 using system. Collections. Generic;
3 using system. text;
4 /**//**/
5/** // <summary>
6 // name: submitoncepage
7 // parent class: system. Web. UI. Page
8 // Description: Page extension class that solves the problem of repeated data submission caused by browser refreshing.
9 // example: If (! This. isrefreshed)
10 ///{
11 ///// code
12 ///}
13 // Original: Cong Xingzi (cncxz) E-mail: cncxz@126.com
14 /// </Summary>
15 namespace spa. Common
16 {
17 public class submitoncepage: system. Web. UI. Page
18 {
19 private string _ strsessionkey;
20 private string _ hiddenfieldname;
21 private string _ strlastviewstate;
22 public submitoncepage ()
23 {
24 _ hiddenfieldname = "_ lastviewstate_sessionkey ";
25 _ strsessionkey = system. guid. newguid (). tostring ();
26 _ strlastviewstate = string. empty;
27
28}
29
30 public bool isrefreshed
31 {
32 get
33 {
34 string str1 = This. Request. Form ["_ viewstate"];
35_strlastviewstate = str1;
36 string str2 = This. session [getsessinkey ()] as string;
37 bool flag1 = (str1! = NULL) & (str2! = NULL) & (str1 = str2 );
38 return flag1;
39}
40}
41
42 protected override void render (system. Web. UI. htmltextwriter writer)
43 {
44 string STR = getsessinkey ();
45 this. session [STR] = _ strlastviewstate;
46 clientscript. registerhiddenfield (_ hiddenfieldname, STR );
47 base. Render (writer );
48}
49
50
51 private string getsessinkey ()
52 {
53 string STR = This. Request. Form [_ hiddenfieldname];
54 return (STR = NULL )? _ Strsessionkey: STR;
55}
56
57
58}
59
60}
61

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.