locked
Page not refreshing after a redirect RRS feed

  • Question

  • User-1466445078 posted
    Hi,

    I have a link on my site that when clicked takes the user to a new page (which is just script/code) that sets a session then returns the user to the original page by way of Response.Redirect to Request.ServerVariables["HTTP_REFERER"]

    When the user returns to the original page some extra content should be visible (based on whether the particular session is set or not). The problem is that you have to force the browser to refresh (after you're redirected back to the original page) to see the extra content - if you don't refresh you don't see the content (even the session is set).

    Is there a way I can make the page "not cache" so that the browser has to check for a new version each time the page is loaded or is there a better way of setting the session and refreshing the page rather than linking to another page, setting the session,and then redirecting back?

    Many thanks.
    Monday, August 4, 2014 11:47 AM

Answers

  • User-760709272 posted

    Add a random parameter to the querystring so the page url is unique and not retrieved from the cache.  Something like

    Response.Redirect (Request.ServerVariables["HTTP_REFERER"] & "?r=" + DateTime.Now.Ticks.ToString());

    you'll have to cater for if there are already params on the referring page though.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 4, 2014 1:08 PM
  • User895691971 posted

    This behaviour is expected only if you're redirecting the user back to the -1 of his history because browser believes the data is still the very same and should be used from the Cache. The back button, for instance. Otherwise, this should not happen. Why don't you stay on the page and then allow the user to redirect back, or do the thing on the very page. Some thing like this

    @{
       // a variable set to true when user clicks the button.
       var sessionSet = false;
       if(IsPost) {
          // submitted the form.
          Session["set"] = true;
       }
       if(Session["set"] != null) {
          // if session is not empty (null)
          sessionSet = true;
       }
       // ... remaining code below or above this block.
    }

    The HTML can be written as the one follows, 

    <html>
       <head>
          <title>My ASP.NET Web Page</title>
       </head>
       <body>
          @if(sessionSet) {
             // if session set, user has done clicking the submit button
             <h4>Thanks for setting the session</h4>
             <p>You have had done setting the session variable by clicking the submit button last time.</p>
             <p>The content was: "Love for all, Hatred for none!"</p>
          } else {
             <p>You need to create the session variable, to view the content.</p>
             <form method="post">
                <input type="submit" value="Create Session" />
             </form>
          }
       </body>
    </html>

    This is just the example. You can also trigger this command on any other event too. Hopefully, this would work for you. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 4, 2014 1:11 PM

All replies

  • User-760709272 posted

    Add a random parameter to the querystring so the page url is unique and not retrieved from the cache.  Something like

    Response.Redirect (Request.ServerVariables["HTTP_REFERER"] & "?r=" + DateTime.Now.Ticks.ToString());

    you'll have to cater for if there are already params on the referring page though.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 4, 2014 1:08 PM
  • User895691971 posted

    This behaviour is expected only if you're redirecting the user back to the -1 of his history because browser believes the data is still the very same and should be used from the Cache. The back button, for instance. Otherwise, this should not happen. Why don't you stay on the page and then allow the user to redirect back, or do the thing on the very page. Some thing like this

    @{
       // a variable set to true when user clicks the button.
       var sessionSet = false;
       if(IsPost) {
          // submitted the form.
          Session["set"] = true;
       }
       if(Session["set"] != null) {
          // if session is not empty (null)
          sessionSet = true;
       }
       // ... remaining code below or above this block.
    }

    The HTML can be written as the one follows, 

    <html>
       <head>
          <title>My ASP.NET Web Page</title>
       </head>
       <body>
          @if(sessionSet) {
             // if session set, user has done clicking the submit button
             <h4>Thanks for setting the session</h4>
             <p>You have had done setting the session variable by clicking the submit button last time.</p>
             <p>The content was: "Love for all, Hatred for none!"</p>
          } else {
             <p>You need to create the session variable, to view the content.</p>
             <form method="post">
                <input type="submit" value="Create Session" />
             </form>
          }
       </body>
    </html>

    This is just the example. You can also trigger this command on any other event too. Hopefully, this would work for you. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 4, 2014 1:11 PM
  • User-1466445078 posted
    Hi,

    Thanks AidyF and Afzaal.

    Your explanations make perfect sense and I'll give both answers a try to see what works works best.

    Many thnaks.
    Monday, August 4, 2014 1:42 PM