I am working with an asp (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.
I am working with an asp (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.
Share Improve this question edited Feb 8, 2022 at 9:31 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 19, 2011 at 18:07 KIRAN K JKIRAN K J 7325 gold badges30 silver badges62 bronze badges 3-
This is like asking how to b 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 Commented 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. – MoarCodePlz Commented Aug 19, 2011 at 18:09
- at last i got the exact question.How to clear cache using javascript or c# ? – KIRAN K J Commented Aug 20, 2011 at 12:11
3 Answers
Reset to default 2you 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 pleteing 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";
}
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.
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 remend you spend some time studying the application Directives; mainly Page and Control.