How to Detect Refresh or Postback in ASP.NET

 

We are always checking Ispostback value. IsPostback will always have the value which was set previously. So for instance, if the page was posted back before refresh, then the value will be true and if the page is not posted back before refresh, then the value will be false.

protected void Page_Load(object sender, EventArgs e)   

{       

if (!IsPostBack)       

{           

Session[“PostID”] = “1001”;           

ViewState[“PostID”] = Session[“PostID”].ToString();           

Response.Write(“First Load”);       

}       

else       

{           

if (ViewState[“PostID”].ToString() == Session[“PostID”].ToString())           

{               

Session[“PostID”] = (Convert.ToInt16(Session[“PostID”]) + 1).ToString();

ViewState[“PostID”] = Session[“PostID”].ToString();
  Response.Write(“Postback”);

  }
else
{
   ViewState[“PostID”] = Session[“PostID”].ToString();               

Response.Write(“refreshed”);

 } 

}
}

Leave a Reply

Your email address will not be published. Required fields are marked *