Avoid execution of page While you Refresh

Posted by Venkat | Labels: , , ,

Hi , here we have to discuss the how to avoid the execution of code

While you refresh the page.. suppose if you have written the code for sending email to users.. after clicking the button the code has been executed suppose if youRefresh the same page once again the Email sent to the user so here we have to avoid the execution on code on page_Refresh

this is the code in VB.NET and C#

Vb.NET


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
End If
End Sub


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If Session("CheckRefresh").ToString() = ViewState("CheckRefresh").ToString() Then
Label1.Text = "Hello"
Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
Else
Label1.Text = "Page Refreshed"
End If
End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
ViewState("CheckRefresh") = Session("CheckRefresh")
End Sub

Here in this code i m saving system date and time in session varaible when the page gets load and in the page prerender event , which occurs after page_Load event , i m assigning the value if session variable to viewstate varaible
Now in Button_Click Event i m checking if both the values in session variable and in ViewState varaible are same than page in not refreshed
if they are not same that means page has got refreshed

This is code for C#.NET


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"];
}

0 comments:

Post a Comment

Thanks for the Comments.

PayOffers.in