To Prevent Duplicate Event on page after insert or update if user press F5 to refresh page you can handle it using this code sample
1- in page_load when !postback create session and save datetime to that session
if (!IsPostBack)
{
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
2- save the session value to view State on Page_PreRender Event like this
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["update"] = Session["update"];
}
3- on Button_Click Event You will Check That viewstate value equal to session value if true do action and then update session value with the new datetime .
on the second time the condition will not executed because of change in value between session value and viewstate value.
protected void BTN_SEND_Click(object sender, EventArgs e)
{
if (Session["update"].ToString() == ViewState["update"].ToString())
{
//write your action code here
Session["update"] = Server.UrlEncode(DateTime.Now.ToString());
}
else
{
//write exception that no duplicate allowed or do any thing else.
}
}
1- in page_load when !postback create session and save datetime to that session
if (!IsPostBack)
{
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
2- save the session value to view State on Page_PreRender Event like this
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["update"] = Session["update"];
}
3- on Button_Click Event You will Check That viewstate value equal to session value if true do action and then update session value with the new datetime .
on the second time the condition will not executed because of change in value between session value and viewstate value.
protected void BTN_SEND_Click(object sender, EventArgs e)
{
if (Session["update"].ToString() == ViewState["update"].ToString())
{
//write your action code here
Session["update"] = Server.UrlEncode(DateTime.Now.ToString());
}
else
{
//write exception that no duplicate allowed or do any thing else.
}
}
No comments:
Post a Comment