Posted by: mouneir | November 27, 2010

Refresh Problem Asp.Net

SA,
How do I prevent previously submitted form data from being reinserted into the database when the user presses the browser’s Refresh button?

- there are a little solutions but i think the next one is the best :
solution : you need to determine that the user has refreshed the page in the browser instead of pressing the form’s submit button.
A simple way to implement refresh trapping is by the use of a date/time stamp held in a ViewState variable and a date/time stamp held in the user’s Session. On the page’s PreRender event, a ViewState variable is set to the value of the Session variable. These two values are compared to each other immediately before the database INSERT command is run.
If they are equal, then the command is permitted to execute and the Session variable is updated with the current date/time, otherwise the command is bypassed.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Update"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
}

protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["Update"] = Session["Update"];
}

protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Update"].ToString() == ViewState["Update"].ToString())
{
// Your Code
Session["Update"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
else
{
// Error
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.