Search This Blog

Wednesday, August 10, 2011

Sunday, August 7, 2011

Preventing Re-submission of data on Browser Refresh

Avoid Duplicate record insert on page refresh using ASP.NET


Code
VB.net


Dim IsPageRefresh As Boolean = False
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


If Page.IsPostBack Then
            If ViewState("postid").ToString() = Session("postid").ToString() Then
                IsPageRefresh = True
 End If

Session("postid") = System.Guid.NewGuid().ToString()
        ViewState("postid") = Session("postid")

End Sub


Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        If Page.IsValid And IsPageRefresh Then
' Write your Code here
End Sub

C#


bool IsPageRefresh = false;

protected void Page_Load(object sender, System.EventArgs e)
{
 if (Page.IsPostBack) {
  if (ViewState("postid").ToString() == Session("postid").ToString()) {
   IsPageRefresh = true;
  }
 }
 Session("postid") = System.Guid.NewGuid().ToString();
 ViewState("postid") = Session("postid");

}

protected void btnSubmit_Click(object sender, System.EventArgs e)
{
 if (Page.IsValid & IsPageRefresh) {
  // Write your Code here
 }
}