Its very common problem every web developer has to face that is re-submitting data on page refresh (F5).
You will find lots of solution whiling googling but some of them are very lengthy and some are very confusing. I found very easy and effective way of handling this problem.
Using session[] and viewstate[]
On page load take one session variable and store system current date time
if (!IsPostBack)
{
Session["Refresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
On Button click event check whether session and viewstate are same value or not, If same then do your login
if (Session["Refresh"].ToString() == ViewState["Refresh"].ToString())
{
[ Do your coding here ]
Session["Refresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
else
{
response.write(“You have refreshed the page”)
}
Write one Page pre render event to assing session value in viewstate like this
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["Refresh"] = Session["Refresh"];
}
Thats all….happy coding
Advertisement