IsPageRefresh in ASP .NET - Preventing previously submitted


/ Published in: C#
Save to your folder(s)

How do I prevent previously submitted form data from being reinserted into the database when the user presses the browser's Refresh button?
Although it is not possible due to unique key constraint available in tables but it is unnecessary go to other layer (data access, business layer).


Copy this code and paste it in your HTML
  1. // declare a global variable on page
  2. private Boolean IsPageRefresh = false;
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. if (!IsPostBack)
  6. {
  7. ViewState["postids"] = System.Guid.NewGuid().ToString();
  8. Session["postid"] = ViewState["postids"].ToString();
  9. TextBox1.Text = "Hi";
  10.  
  11. }
  12. else
  13. {
  14. if (ViewState["postids"].ToString() != Session["postid"].ToString())
  15. {
  16. IsPageRefresh = true;
  17. }
  18. Session["postid"] = System.Guid.NewGuid().ToString();
  19. ViewState["postids"] = Session["postid"];
  20. }
  21. }
  22. protected void Button1_Click(object sender, EventArgs e)
  23. {
  24. if (!IsPageRefresh) // check that page is not refreshed by browser.
  25. {
  26. TextBox2.Text = TextBox1.Text + "@";
  27.  
  28. }
  29. }

URL: http://www.dotnetspider.com/resources/4040-IsPageRefresh-ASP-NET.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.