Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
There is one textbox,submit button,grid on webform.

When i click on submit button it will add value of textbox to grid.

Problem is if i press F5 after that it again submit to grid.

How do i avoid that.
Posted

Apart from replies above, have a look at these articles:
Refresh Page Issue in ASP.Net[^]
Stop Refresh after Submitting your Request[^]
Detecting Page Refresh[^]
 
Share this answer
 
Comments
raju melveetilpurayil 30-Jul-10 2:16am    
thanks for some good links :)
Redirect your page back to itself, then the refresh will do the redirect, not the postback.
 
Share this answer
 
Want a simple working solution?
This one works perfectly with multiple clicks on button and so on.
http://csharpdotnetfreak.blogspot.com/2008/12/detect-browser-refresh-to-avoid-events.html
PS. No it's not an ad for my solution, just want to help

C#
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CheckRefresh"] =
Server.UrlDecode(System.DateTime.Now.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["CheckRefresh"].ToString() ==
ViewState["CheckRefresh"].ToString())
{
Label1.Text = "Hello";
Session["CheckRefresh"] =
Server.UrlDecode(System.DateTime.Now.ToString());
}
else
{
Label1.Text = "Page Refreshed";
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["CheckRefresh"] = Session["CheckRefresh"];
}
}
 
Share this answer
 
v3
Why you do not use Ajax. I thing Ajax will be good for you.
 
Share this answer
 
Check the article
Stop Refresh after Submitting your Request[^]

cheers
 
Share this answer
 
// declare a global variable on page
private Boolean IsPageRefresh = false;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
TextBox1.Text = "Hi";

}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"];
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!IsPageRefresh) // check that page is not refreshed by browser.
{
TextBox2.Text = TextBox1.Text + "@";

}
}
 
Share this answer
 
You should add validation before adding value to grid. Alternatively you can clear textbox value after submitting data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900