avoid button click event from firing when page refresh occurs in asp.net

Posted by Sekar.C under ASP.NET on 10/4/2012 | Points: 10 | Views : 10037 | Status : [Member] | Replies : 1
Hi,
Avoid button click event from firing when page refresh occurs in asp.net.

Regards
Sekar.c



Responses

Posted by: Britsouls on: 10/7/2012 [Member] Starter | Points: 25

Up
0
Down
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // If page loads for first time
{
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); // Assign the Session["update"] with unique value

//=============== Page load code =========================

//============== End of Page load code ===================
}

}

protected void Button1_Click(object sender, EventArgs e)
{
if (Session["update"].ToString() == ViewState["update"].ToString()) // If page not Refreshed
{
//=============== On click event code =========================

Label1.Text = TextBox1.Text;
//lblDisplayAddedName.Text = txtName.Text;

//=============== End of On click event code ==================

// After the event/ method, again update the session
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
}
else // If Page Refreshed
{
// Do nothing
}
}
protected override void OnPreRender(EventArgs e)
{
ViewState["update"] = Session["update"];
}

}

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response