Results 1 to 5 of 5

Thread: ASP.NET how to prevent duplicate submission!

  1. #1
    Join Date
    Feb 2009
    Posts
    36

    ASP.NET how to prevent duplicate submission!

    i am working on project for which i need some help can here some one tell me how to prevent users under many consecutive click BUTTON after data inserted into the database

  2. #2
    Join Date
    Dec 2008
    Posts
    196

    Re: ASP.NET how to prevent duplicate submission!

    Add a hidden. Generated form, of a unique generation. In the preservation of real data before the look of the hidden values are vested in the form has been submitted.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: ASP.NET how to prevent duplicate submission!

    Concrete steps are as follows:
    1, enter the page at the time of randomly generated strings, the saved to the session and wrote the controls on the page.
    2, in the submission, the judge of your session in the string and the string is submitted to the same, the same on the deal; not duplicate the same description are submitted, not related to treatment, write a reminder alert should not submit duplicate.
    3, then repeat Step 1.
    The following is a test page's source code.
    Test.aspx
    <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "test.aspx.cs" Inherits = "test"%>
    <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title> test </ title>
    </ head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    <asp:HiddenField ID="HidSign" runat="server" />
    </ div>
    </ form>
    </ body>
    </ html>
    test.aspx.cs
    using System;
    public partial class test: System.Web.UI.Page
    (
    protected void Page_Load (object sender, EventArgs e)
    (
    if (! IsPostBack)
    (
    UpdateSign ();
    )
    )
    protected void Button1_Click (object sender, EventArgs e)
    (
    if (Session [ "HidSign"]. ToString () == HidSign.Value)
    (
    this.ClientScript.RegisterStartupScript (this.GetType (), "startup", "alert ( 'Update successful !')", true);
    UpdateSign ();
    )
    else
    (
    this.ClientScript.RegisterStartupScript (this.GetType (), "startup", "alert ( 'Please do not submit duplicate !');", true);
    UpdateSign ();
    )
    )
    private void UpdateSign ()
    (
    HidSign.Value = DateTime.Now.ToString ( "ffffff");
    Session [ "HidSign"] = HidSign.Value;
    )
    )

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: ASP.NET how to prevent duplicate submission!

    Refresh button can also be prohibited, at the click of a button you can cut-card button, set the page expired, and after the Jump to another page, Can use the tokens to achieve the function. Save on the server at Session 1 boolean value, in the submission are to verify the value of Session.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: ASP.NET how to prevent duplicate submission!

    C # code

    public class RefreshServe: System.Web.UI.Page
    (
    private static ILog log = LogManager.GetLogger (typeof (RefreshServe));

    private readonly string REFRESH_TICKET_NAME = "__RefreshTicketArray";
    private readonly string HIDDEN_FIELD_NAME = "__RefreshHiddenField";
    private readonly string HIDDEN_PAGE_GUID = "__RefreshPageGuid";

    / / / <summary>
    / / / Refresh the page for the True said, False normal submit / / / </ summary>
    public bool IsPageRefreshed
    (
    get
    (
    if (IsPostBack & & ! CheckRefreshFlag ())
    (
    log.Debug ( "Refresh the page");
    return true;
    )
    else
    (
    log.Debug ( "normal submit");
    return false;
    )
    )
    )

    / / / <summary>
    / / / Show identification before the update / / / </ summary>
    / / / <param name="e"> </ param>
    protected override void OnPreRender (EventArgs e)
    (
    log.Debug ( "the implementation of OnPreRender");
    base. OnPreRender (e);
    UpdateRefreshFlag ();
    )


    / / / <summary>
    / / / Update the logo, to submit all the normal delete the submission time and the production of the current time / / / </ summary>
    private void UpdateRefreshFlag ()
    (

    # region Cookie mode

    / / Register that uniquely identifies the page and return
    string pageGuid = SetCurPageGuid ();

    HttpCookie cookie = GetRefreshTicket ();

    if (cookie.Values.Count> 0)
    (
    cookie.Values.Remove (pageGuid);
    log.Debug ( "Clear the current change of the cookie is:" + PageGuid);
    )

    string submitTime = DateTime.Now.ToString ( "hhmmss.fffff");
    / / Save the current timing of the submission to Hidden Field
    ClientScript.RegisterHiddenField (HIDDEN_FIELD_NAME, submitTime);


    log.Debug ( "the time will soon want to add: submitTime:" + SubmitTime + "Guid:" + PageGuid.ToString ());
    cookie.Values.Add (pageGuid, submitTime);

    log.Debug ( "UpdateRefreshFlag Medium Cookie currently exist for the Record:" + Cookie.Values.Count);
    for (int i = 0; i <cookie.Values.Count; i + +)
    log.Info ( "cookie [" + Cookie.Values.GetKey (i) + "]:" + Cookie.Values [i]);

    Response.AppendCookie (cookie);

    # endregion

    )


    / / / <summary>
    / / / Verify whether refresh / / / </ summary>
    / / / <returns> </ returns>
    private bool CheckRefreshFlag ()
    (
    HttpCookie cookie = GetRefreshTicket ();
    string pageGuid = GetCurPageGuid ();
    if (cookie.Values.Count> 0)
    (
    bool flag;
    if (cookie.Values [pageGuid]! = null)
    flag = cookie.Values [pageGuid]. IndexOf (GetCurSubmitTime ())> - 1;
    else
    flag = true; / / prevent abnormal always be able to submit
    if (flag)
    log.Debug ( "submit time exist, could be submitted");
    else
    log.Debug ( "invalid submission time");
    return flag;
    )
    return true;
    )


    / / / <summary>
    / / / Get the submission time has been saved, not new, there is return / / / </ summary>
    / / / <returns> </ returns>
    private HttpCookie GetRefreshTicket ()
    (
    # region Cookie mode, return value of Cookie

    HttpCookie cookie;
    if (Request.Cookies [REFRESH_TICKET_NAME] == null)
    (
    cookie = new HttpCookie (REFRESH_TICKET_NAME);
    Response.AppendCookie (cookie);
    log.Debug ( "Cookie does not exist, initialize");
    )
    else
    (
    cookie = Request.Cookies [REFRESH_TICKET_NAME];

    log.Debug ( "Read has been in existence for the Cookie, the current Cookie in the number of records in existence as:" + Cookie.Values.Count + "Concrete has the following:");

    for (int i = 0; i <cookie.Values.Count; i + +)
    log.Info ( "cookie [" + Cookie.Values.GetKey (i) + "]:" + Cookie.Values [i]);
    )
    return cookie;
    # endregion
    )


    / / / <summary>
    / / / Obtain the current time to submit / / / </ summary>
    / / / <returns> </ returns>
    private string GetCurSubmitTime ()
    (
    string submitTime = Request.Params [HIDDEN_FIELD_NAME] == null ? "": Request.Params [HIDDEN_FIELD_NAME]. ToString ();
    log.Debug ( "Executive GetCurSubmitTime: submitTime as:" + SubmitTime);
    return submitTime;
    )


    / / / <summary>
    / / / Settings page Unique logo, through Guid logo to distinguish each page to submit their own time / / / </ summary>
    private string SetCurPageGuid ()
    (
    string guid;
    if (! IsPostBack)
    (
    if (Request.Params [HIDDEN_PAGE_GUID] == null)
    (
    guid = System.Guid.NewGuid (). ToString ();
    log.Debug ( "SetCurPageGuid register a new logo:" + Guid);
    )
    else
    guid = GetCurPageGuid ();

    )
    else
    (
    guid = GetCurPageGuid ();
    )

    ClientScript.RegisterHiddenField (HIDDEN_PAGE_GUID, guid);
    return guid;
    )



    / / / <summary>
    / / / Get the current page of the unique identifier / / / </ summary>
    / / / <returns> </ returns>
    private string GetCurPageGuid ()
    (
    string pageGuid = Request.Params [HIDDEN_PAGE_GUID] == null ? "None": Request.Params [HIDDEN_PAGE_GUID]. ToString ();
    log.Debug ( "Executive GetCurPageGuid () after Page_GUID as:" + PageGuid);
    return pageGuid;
    )

    Necessary to refresh the page to determine the function of the new type can be inherited only through quoted property IsPageRefreshed Recognition "refresh express true and false then it is normal to submit," Writing the operation of the database at if (! IsPageRefreshed)
    (
    Database operations)
    Can, if it is set will not be implemented, part of the code used in the Notes are kept Session ticket, because session is lost and is easier to account for memory, so the use of cookie,

Similar Threads

  1. What do you mean by PAD Submission
    By Ramanujan in forum Technology & Internet
    Replies: 3
    Last Post: 01-11-2011, 11:18 PM
  2. How to prevent duplicate database entries
    By Zombi in forum Software Development
    Replies: 3
    Last Post: 25-09-2009, 02:59 PM
  3. Need a counter for my form submission
    By Kr8zyCanuck in forum Technology & Internet
    Replies: 3
    Last Post: 09-06-2009, 11:18 AM
  4. Replies: 1
    Last Post: 25-03-2009, 05:29 PM
  5. Replies: 0
    Last Post: 08-01-2009, 08:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,236,090.34364 seconds with 17 queries