Asp.net prevents refresh and resubmit database operations

Source: Internet
Author: User

Asp.net prevents refresh and resubmit database operations

Recently, I was writing a B/S application system, using a combination of Asp.net (2.. 0) + windows verification + Ajax + sql2005 + vs2008 + Windows2003.
Because we are dealing with internal customers (two little girls from finance departments) in the company, we did a few input interfaces and asked them to test.
Refresh the browser system after new or modified data is found to repeat the previous time (the most recent database operation ).
Here we will share with you the solutions to solve this problem.

My thinking is:
Search on Google on Baidu, but several solutions are not suitable for me.
Online solutions include:
1. Jump back to this page after database operations.
After the data operation is successful: This. Page. response. Redirect = "URL on this page ";
This solution can prevent refresh and resubmit database operations, but it is denied by the Financial girl because the page Jump to this page is always redirected to the first page by default, so that he can see

If you do not have the data you just entered, go to the last page.
2. Mark with viewstate
After submission, viewstate ["issubmit"] = false;
This does not work because viewstate ["issubmit"] is implemented using hiddenfield, and the data submitted during refresh is the previous data.

Viewstate ["issubmit"] is also the previous value.
3. If you use the session function, you cannot determine whether the user is Refresh or resubmit.
No way. If none of the above methods work, you only need to find a solution.
My idea is:
1. Since you submitted the data for the previous time, you should mark it for me (to be unique), but the record value itself cannot be marked, because it is entirely possible to insert or modify the same record

.
2. Finally, I decided to use a hiddenfield (ID is issubmit) to remember this identifier. The method to generate this unique value is:

// Issubmit

<Asp: hiddenfield id = "issubmit" runat = "server" value = "0"/>

// Insert
<Asp: button id = "button1" runat = "server" causesvalidation = "true"
Commandname = "insert" text = "insert" onclientclick = "javascript: Document. getelementbyid

('Issubmit '). value = (new date (). gettime () "/>

// Update
<Asp: button id = "button1" runat = "server" causesvalidation = "true"
Commandname = "Update" text = "Update" onclientclick = "javascript: Document. getelementbyid

('Issubmit '). value = (new date (). gettime () "/>
Note: The above function is to assign the value of hiddenfield (ID is issubmit) to the current number of milliseconds of the system when inserting and updating (it should be unable to submit and refresh again within milliseconds ).

In theory, this value is always unique for a session ).
3. Check my server actions (insert)
Protected void sqldatasource3_inserting (Object sender, sqldatasourcecommandeventargs E)
{
If ("" + this. session ["lastsubmitid"] = "")
{
This. session. Add ("lastsubmitid", this. issubmit. Value); // if this session does not exist, it indicates that it has not been submitted before. It must not be repeated. Allow

But you need to record this. issubmit. value to this. session ["lastsubmitid"] For the next comparison

}
Else
{
If ("" + this. session ["lastsubmitid"] = "" + this. issubmit. Value) // if this. session ["lastsubmitid"] exists and

This. issubmit. If the value is equal, it indicates that it is refreshed. Because the last data submitted is refreshed, the value of this. issubmit. value will not change, and the data operation will be skipped.
{
E. Cancel = true;
}
Else // if this. session ["lastsubmitid"] exists but is not the same as this. issubmit. value, it indicates that the operation is performed by clicking insert or update data.

This operation is usually performed, but this. issubmit. value must be recorded in this. session ["lastsubmitid"] for future comparison.

{
This. session ["lastsubmitid"] = "" + this. issubmit. value;
}
}

}
Update Is Not explained. The same is true.
Protected void sqldatasource3_updating (Object sender, sqldatasourcecommandeventargs E)
{
If ("" + this. session ["lastsubmitid"] = "")
{
This. session. Add ("lastsubmitid", this. issubmit. value );

}
Else
{
If ("" + this. session ["lastsubmitid"] = "" + this. issubmit. value)
{
E. Cancel = true;
}
Else
{
This. session ["lastsubmitid"] = "" + this. issubmit. value;
}
}
}

4. It must have been said again. Every database operation needs to insert such a pile of code, which is too annoying. we can write a method to verify whether the code is submitted normally to call the code.

Annoying.
For example:
Private bool checklastsubmitid () // verify whether submission is normal
{
If ("" + this. session ["lastsubmitid"] = "")
{
This. session. Add ("lastsubmitid", this. issubmit. value );
Return true;
}
If ("" + this. session ["lastsubmitid"] = "" + this. issubmit. value)
{
Return false;
}
Else
{
This. session ["lastsubmitid"] = "" + this. issubmit. value;
Return true;
}
}
The code for the above two events is changed:
Protected void sqldatasource3_updating (Object sender, sqldatasourcecommandeventargs E)
{
If (! Checklastsubmitid ())
{
E. Cancel = true;
}
}
Protected void sqldatasource3_inserting (Object sender, sqldatasourcecommandeventargs E)
{
If (! Checklastsubmitid ())
{
E. Cancel = true;
}
}
Don't bother, haha.

The level is limited and the writing is not good. Thank you for your patience.

 

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.