How to completely refresh (ctrl+f5) a page using asp.net? - javascript

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.

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";
}

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 recommend you spend some time studying the application Directives; mainly Page and Control.

Related

Click a button on a page to increment a counter in another page using PHP?

so as the title says, I've a button on my main page which should send the amount of times clicked to another page.
I know how to do this via JS, but not in PHP. PHP is also not live, so I'd need to refresh the webpage in order to see the effect, I've been reading about it and I might try and mess around with AJAX to refresh a certain element of my webpage.
Is it possible to do this?
I think you should save it somewhere because PHP is handled server side. So either a cookie/session or a database.

Interim message when downloading a file

On my website I have an option to display some data and also to download it in csv format. Some of the data is quite large (20,000 - 900,000 rows in SQL). When I display it on the site I use paging so that it quickly displays only x amount of rows at a time, however, the download link of course should and is downloading the entire report which can take several seconds to a couple of minutes depending on file size. I'm wondering if there is a way to create an interim pop-up or message in-line that says something like "Gathering Information..." where I could additionally put an animated gif so the user knows something is happening. Creating this shouldn't be an issue but I'm not sure if there is a way to trigger that to disappear once the download pop-up appears. The solutions I've seen on this site all suggest using a timer, but thats not an option in this case as the times will vary a lot.
I'm using classic ASP so would like to use either that or JavaScipt. I could additionally use flash if makes a difference.
I would probably wire up the "Gathering info..." message to be hidden upon receipt of a Comet style message from the server that could be sent out as soon as the file is ready to download on its end.
The gist of it is to use a cookie that you set using pure javascript or jquery cookies for ease of use. Then you send the cookie's name to your server, which sends an update for said cookie with the file once delivered.
Meanwhile you have some JS code that checks periodically for the cookie to see if its updated, and can then update the layout accordingly.
I ended up doing this a different way. On my site I have page we'll call "default.asp" on that page is a link to another asp wel'll call "download.asp". download.asp is where the code is to do the SQL query and create the report, which means this is where the wait comes in. After the report is prepared the popup to save, open, etc is displayed. What I ended up doing is creating a div that by default is hidden. When the download button is clicked the div is unhidden, then just before the popup is displayed I once again hide the div. Hopefully this can be of help to someone else as well.

General principles for page reload with counter and different data

The website I need to implement is for a simple user study: On a page there are some images shown, the user rearranges them to a certain order, hits the 'next' button and then the same page is shown again, just with different images, which the user rearranges again. The process is repeated until a certain page count is reached.
Which images to show, comes from a mysql database and the user arrangements for each page need to be stored in the database as well. I have decent code for the database communication and logic of the image arrangements in php. The ordering functionality of the images works nicely with javascript and jquery.
What is completely throwing me off now, is bringing it all together and the 'save and reload the page with different images' mechanism. Of course I found loads of information on the internet but I just can't bring it together (I am a noob with javascript and that stuff is seriously making my head hurt).
My question:
How do I implement a page, with a page counter, which is increased when I press a button; pressing the button also triggers sending the data to php for putting it in the database and also reloads/updates the page with a different image selection based on the page counter to re-start the process.
I would be most greatful for all: from explaining the general principles to specific code examples. I just need to make this thing work :). Thanks!
You need a < form method=POST > tag which wrapps all your images
For each image you need a < input type="hidden" > tag which stores the image id. This tag must be resorted with the images as well.
Resorting must change the position in the DOM
you need a submit button as well
On server side you can access the submitted data with $_POST variable in same sorting as in the DOM
You can count the number pages in $_SESSION variable or send the counter as GET variable with the page url
This is only a short overview, i hope this helps you.

Javascript: Making divs hold state of display='block' when user clicks BACK button in browser

I have a few divs on a form that are hidden by default (style="display:none;"). When the user clicks a certain radio button, an onclick event executes and exposes the divs. The user is then taken to a review page upon form submit that shows him any errors. If there are any, he clicks the BACK button on his browser to go back to the form and correct them. Caching is enabled so that all of his form contents are there. The problem is, since the form is looking for an onclick event, all of the previously exposed divs are hidden again. Is there any way to make sure they stay exposed when the user clicks back to the form from the review page? I thought a document.ready function would do it, but no joy.
As Yair mentioned, you can use cookies. It cannot be done with pure JS. However, you can also use PHP.
Before the user is transferred to the second page, have JS scan the divs in question, and find which ones are visible. (I'm assuming they all have individual IDs). Store these IDs in a comma-delimited string, or array, and send it as a _POST or _GET to the new page.
Have PHP store it as a hidden value somewhere. You could use a hidden input, or a data-x on something ... as long as it's there.
Have JS on that page that watches for the back click, stops it, and then redirects the user to the previous page, and sends the string or array back to it. Have PHP on that page print it as a JS value, and have JS on pageload show all divs with matching IDs.
Cookies or localStorage if you aim for only modern browsers:
localStorage
Is there any way to make sure they stay exposed when the user clicks
back to the form from the review page? I thought a document.ready
function would do it, but no joy.
You can use cookies in order to manage state in a web-browser. Cookies will help you save the desired user's state.
All javascript code is reinitialized on browser reload. You cannot identify whether the user comes back through the browser.
You can use cookies or local storage to save a value when initial display happens and show/hide the div later on document.ready.

Is Javascript code being processed after clicking on a submit button?

I am working on a ECommerce Project. During the checkout process there is the button to purchase the item at the end of the checkout process. Clicking on that button submits the form on the page using a POST request (Standard, no AJAX). However, if you click on that submit button multiple times really quick, multiple POST Requests are sent to the server and so there are also multiple Orders being created.
My Question: Is it save to prevent these multiple button clicks by javascript, or is the page in a weird state at some point because the new page load kicks in?
Is there a difference about that if I do it with inline script:
onsubmit="if(submitted) return false; submitted = true; return true"
Or in my JS external file?
Another option would be to disable the submit button right after the form submit, but IE adds really Ugly Font Color to the button, which cannot be removed by CSS.
The page continues to process events and run javascript until the form Post returns new content which then replaces the current document, thus loading a new document. It is not in a weird state and works normally until the new content arrives and the current document is closed.
There are various techniques for preventing multiple presses of the button. Your code can keep track of the fact that a post has already been sent (in a global variable) and block any more being sent whenever that variable is set. In the UI, you can disable the button as soon as the first post is initiated so the button can't be pressed again.

Categories

Resources