Tuesday, April 10, 2012

Preventing Re-submission of data on Page/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
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 If
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
 }
}
 
 
Source:- OOPS-Solution by Rashmi Sir

No comments:

Post a Comment