Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I have a method in Page_Load(object sender, EventArgs e) that generates some dynamic html codes.
After navigating the page to another page, When I click the back button of my IE, I wont have my generated html. The problem is Page_Load doesn't run again.

How can I solve this problem.

C#
protected void Page_Load(object sender, EventArgs e)
{
    CreateHTMLControls();
}
Posted

Hi,

Place this code in Page_Load event:

C#
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;


Regards
Joachim
 
Share this answer
 
The reason for the page executing doesn't affect the page cycle, the Load event always fires when the page is executed.

So, if the Page_Load doesn't run sometimes, it is because the page is cached and doesnt execute on the server. The page can be cached in the browser, in a router somewhere along the way, or on the server using server side page caching

Try disabling output cache and see if the problem still occurs:
XML
<system.web>   
  <caching>     
   <outputCache enableOutputCache="false"/>
  </caching> 
<system.web>
 
Share this answer
 
Comments
Fardan 21-Feb-12 4:03am    
I used it in my Web.config but Page_Load is not runing yet.
As an alternative, add the below code in Page_Load method to retain the content on back operation.

C#
if (this.Master.Page.Header != null && Session["RELOAD"] == null) 
{ 
 System.Web.UI.HtmlControls.HtmlHead hh = this.Master.Page.Header; 
 System.Web.UI.HtmlControls.HtmlMeta hm = new System.Web.UI.HtmlControls.HtmlMeta(); 
 hm.Attributes.Add("http-equiv", "Refresh"); 
 hm.Attributes.Add("content", "3"); 
 hh.Controls.Add(hm); 
}
 
Share this answer
 
Comments
Fardan 21-Feb-12 3:40am    
Thank you. I thought it worked but it was refreshing the page alternately.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900