![]() |
|||
How to prevent Duplicate record post on page refresh in asp.net
By: rekha singh | 21 Apr 2010 5:47 pm
Today I have faced a problem, when I press save button it works fine and then refresh(F5 or Refresh button on browser) the page but this time it is execute the button click event again. For this duplicate data posting on the server. CommentsAssume that in Test.aspx page you have a button and a TextBox: <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="110px" OnClick="btnSubmit_Click" />
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
In Test.aspx.cs..:
public partial class Test : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
btnSubmit.Click += new EventHandler(btnSubmit_Click);base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void Save_Click(object sender, EventArgs e)
{
if (CheckIfDuplicateRequest())
{
return;
}
//Your code to insert the value of the SomeText textbox into the database.
//Set the value of one of the key fields in your web form in session
Session["Refresh"] = TxtName.Text;
}
private Boolean CheckIfDuplicateRequest()
{
if (Session["Refresh"] == null)
{
//Probably submitting the page for the first time
return false;
}
String previousValue = Session["Refresh"].ToString();
if (previousValue != TxtName.Text)
{
//Submitting the page with a different set of values
return false;
}
//Duplicate request.
return true;
}
}
By: rekha singh | 21 Apr 2010
|
