How to Avoid Re-Submitting Data on Page Refresh?

In this article. I will explain how to prevent re-submitting data on browser page reload or refresh in your asp.Net webform using C#. Sometimes we have some web pages through which we submit some user data to the database one after another on the same page. But the problem is, when we submit one user data and then refresh/reload our browser, what happens? The previously submitted data is re-submitted to the database. Here i am going to tell you how to prevent that.

There are following controls are taken in the WebForm:
1. TextBox TextBox1
2. Button SubmitBtn
And my work is when we type something in the TextBox1 and click on the Submit Button .The page shows the given text of the TextBox1. And then if I refresh/reload the page it shows that the page is refreshed, not shows the text again. To do this I've used Session, ViewState
and PreRender Event of the page.

Hereunder is the C# Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
Session["Time"] = DateTime.Now.ToString();
protected void Page_PreRender(object sender, EventArgs e)
ViewState["Time"] = Session["Time"];
protected void SubmitBtn_Click(object sender, EventArgs e)
if (Session["Time"].ToString() == ViewState["Time"].ToString())
// Code for submitting data....
Response.Write(TextBox1.Text + " Submitted!");
TextBox1.Text = null;

Session["Time"] = DateTime.Now.ToString();

// Code for page refresh.
TextBox1.Text = null;
Response.Write("Page Refreshed!");

Related Articles

More articles: Avoid postback Reload page


Author: Arivazhagan Sekar31 May 2011 Member Level: Gold   Points : 0

Good, and thanks.

Guest Author: puneet09 Jun 2012

i spent 7 days on net searching answer for it ,, even bigger forum then dotnetspider doesnt have a answer to thid question ,,,,,, but yours answer is perfect ...... and it solves my 7 days problem in 7 minutes ..... thanks a lot

Guest Author: rahul04 Jul 2012

Thank's working....i solved this problem just becoz of u....thnx..alot...

Guest Author: bheema09 Jan 2013

Superb...ur r gr8

Guest Author: Dara21 Feb 2013

It is still not good yet. I test it with ASPxGridView. When I click on submit button the data is inserted into table and display in ASPxGridView but when I refresh page the data in ASPxGridView is lost.

Guest Author: rtz11 May 2013

Is There Anyone Who Is Getting A Null Reference Exception For Session["time"] Upon Postback.

Guest Author: sakthi sudhan08 Aug 2013

how to avoid the reload in button click event without using view and session state using

Guest Author: Bharath23 Sep 2013

I have used the same logic it works for me,but the problem is after 7 to 8 refresh of the page, the viewstate value becomes null.Not able to understand the problem.Help is appreciated.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: