3

I am working with an asp.net (Dotnetnuke) project now. I need to change an image in a page dynamically. While clicking replace button (asp:Button), some times the image cannot replace dynamically on the page. While pressing ctrl+F5, it will change. My question is, how to reload cache through C# code?

Another problem is that, sometimes I replace an image in a page by storing some values to database and press ctrl+F5 for making changes in the page, but while clicking ctrl+F5 there shows a dialog box with cancel or retry buttons (both on Firefox and IE). While clicking any one of them will store the same value to database. If we again refresh the page the value in the database is 3 times.

3
  • This is like asking how to comb your hair using a carwash ;) You can refresh a page using javascript's location.reload(), but it doesn't affect browser cache. You can prevent browser cache by setting HTTP headers on your images or by requesting images using a different GET string.
    – Blaise
    Aug 19, 2011 at 18:09
  • If you explain what you are actually trying to do (ie: end result) we may be able to give you some insight as to a potentially better approach. Aug 19, 2011 at 18:09
  • at last i got the exact question.How to clear cache using javascript or c# ?
    – KIRAN K J
    Aug 20, 2011 at 12:11

3 Answers 3

2

you cannot clear browser cache.the only idea is declare a session variable in c# code in page load and set its value is 1 at the very first time

if (!IsPostBack)
            {
Session["refresh"]="1"
}

you will need to set session variable in image upload button event Session["refresh"]="1" then create a refresh button .in the button event do the following thats all.after completeing your upload,click on the refresh button.then it work as ctrl+f5 button.if you not set the session value 0 in refresh button event the last event is again takesplace.if you enter a value in database,the same task takesplace if you not set session variable 0.

   if(Session["refresh"].ToString()=="1")
   {
      Response.Write("<script type='text/javascript'>locaton.reload()</script>");
      Session["refresh"]="0";
    }
2

JavaScript cannot access dumping the cache for you, sorry. C# runs of the server, not the client, so it can't help either.

If you want to break the cache for certain items in the page you can add a random parameter

<img src="a.gif?x=123456"> (I like to use a time-stamp)

Alternately, if this is for testing purposes, turn off the cache in your browser.

1
  • the images are dynamic.so i cannot use the above.How to clear cache using javascript or c# ?
    – KIRAN K J
    Aug 20, 2011 at 12:12
1

You can manipulate the cache charateristics of various portions of your web page by managing the user controls. Also check your page directives.

 <%@ OutputCache Duration="#ofseconds" Location="Any | Client | Downstream | Server | None" Shared="True | False" VaryByControl="controlname" VaryByCustom="browser | customstring" VaryByHeader="headers" VaryByParam="parametername" %>

The caching functionality of ASP.NET is pretty extensive. I would recommend you spend some time studying the application Directives; mainly Page and Control.

1
  • Dotnetnuke offers the caching option above so I am not sure why are limiting your options to cutom code. I had an issue with advertising ads being cached locally and it was resolved with the above method. Each ad space must be it's own control.
    – Menefee
    Aug 22, 2011 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.