Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to stop Page being cached

How to stop Page being cached

Scheduled Pinned Locked Moved ASP.NET
tutorial
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NET India
    wrote on last edited by
    #1

    Hello All, How to avoid page being cached. Actually i'm creating an Admin Panel so as i click on Log Out button it brings me over the Default page for Log-in again. But the back button of the browser still working and i want to stop it. For it i'm using the following code. But page is still being cached. Response.Buffer = true; Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d); Response.Expires =-1500; Response.CacheControl = "no-cache";

    S D 2 Replies Last reply
    0
    • N NET India

      Hello All, How to avoid page being cached. Actually i'm creating an Admin Panel so as i click on Log Out button it brings me over the Default page for Log-in again. But the back button of the browser still working and i want to stop it. For it i'm using the following code. But page is still being cached. Response.Buffer = true; Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d); Response.Expires =-1500; Response.CacheControl = "no-cache";

      S Offline
      S Offline
      SomeGuyThatIsMe
      wrote on last edited by
      #2

      there are meta tags you could use, or since you're logged in i assume there is some sort of session varible involved, which could be checked for on page load, and removed on logout, so that if the var isnt present anymore(it expired, or the user logged out) it would redirect to the login screen.

      Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      N 1 Reply Last reply
      0
      • S SomeGuyThatIsMe

        there are meta tags you could use, or since you're logged in i assume there is some sort of session varible involved, which could be checked for on page load, and removed on logout, so that if the var isnt present anymore(it expired, or the user logged out) it would redirect to the login screen.

        Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

        N Offline
        N Offline
        NET India
        wrote on last edited by
        #3

        Here i'm sending my all code for checking at page load still it's not working if (!Page.IsPostBack) { Response.Buffer = true; Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d); Response.Expires =-1500; Response.CacheControl = "no-cache"; if (Session["UserName"] != null) { try { lblMsg.Text = Session["Type"].ToString()+" ("+ Session["UserName"].ToString()+")"; } catch { Server.Transfer("../Admin/Default.aspx"); } } else { Server.Transfer("../Admin/Default.aspx"); } }

        S 1 Reply Last reply
        0
        • N NET India

          Here i'm sending my all code for checking at page load still it's not working if (!Page.IsPostBack) { Response.Buffer = true; Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d); Response.Expires =-1500; Response.CacheControl = "no-cache"; if (Session["UserName"] != null) { try { lblMsg.Text = Session["Type"].ToString()+" ("+ Session["UserName"].ToString()+")"; } catch { Server.Transfer("../Admin/Default.aspx"); } } else { Server.Transfer("../Admin/Default.aspx"); } }

          S Offline
          S Offline
          SomeGuyThatIsMe
          wrote on last edited by
          #4

          I use Response.Redirect instead of Server.Transfer..which seems to be a general consensus but the reason why escapes me at the moment. You dont want to put your session checking code in a !postback block. you want to check every time so that when the session expires you dont get Null reference exceptions if its used outside of a try catch. what you have should work for the first time a page loads, but wont if the user does anything while they're on that page. what happens if you get rid of the !Page.IsPostBack if statement, and run the Response lines and the if(Session["UserName"] != null on every page load?

          Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

          N 1 Reply Last reply
          0
          • S SomeGuyThatIsMe

            I use Response.Redirect instead of Server.Transfer..which seems to be a general consensus but the reason why escapes me at the moment. You dont want to put your session checking code in a !postback block. you want to check every time so that when the session expires you dont get Null reference exceptions if its used outside of a try catch. what you have should work for the first time a page loads, but wont if the user does anything while they're on that page. what happens if you get rid of the !Page.IsPostBack if statement, and run the Response lines and the if(Session["UserName"] != null on every page load?

            Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

            N Offline
            N Offline
            NET India
            wrote on last edited by
            #5

            Sry sir i m not understanding plz explain a bit more............and i think i've already put my code here

            S 1 Reply Last reply
            0
            • N NET India

              Sry sir i m not understanding plz explain a bit more............and i think i've already put my code here

              S Offline
              S Offline
              SomeGuyThatIsMe
              wrote on last edited by
              #6

              you have

              if (!Page.IsPostBack)
              {
              Response.Buffer = true;
              Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
              Response.Expires =-1500;
              Response.CacheControl = "no-cache";

              if (Session["UserName"] != null)
              {
              try
              {
              lblMsg.Text = Session["Type"].ToString()+" ("+ Session["UserName"].ToString()+")";
              }
              catch
              {
              Server.Transfer("../Admin/Default.aspx");
              }
              }
              else
              {
              Server.Transfer("../Admin/Default.aspx");
              }
              }

              correct? what you might try is

              Response.Buffer = true;
              Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
              Response.Expires =-1500;
              Response.CacheControl = "no-cache";

              if (Session["UserName"] != null && Session["Type"] != null)
              {
              lblMsg.Text = Session["Type"].ToString()+" ("+ Session["UserName"].ToString()+")";
              }
              else
              {
              Response.Redirect("../Admin/Default.aspx");
              }

              you only need to have type in the if statement if there is a chance it could be null whitout the UserName bieng null. notice that there is no if(!Page.IsPostBack) statement, this way the code will run EVERY postback, if the session times out and the user tries something they will be redirected to the login(i assume default.aspx is your login page, if its in the same directory as the current page you dont need the relative path just Default.aspx) the nocache options are always set so the back button is effectivly broken(provided they work correctly). this should work, but if the nocache part doesnt, try using meta tags instead.

              Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

              N 1 Reply Last reply
              0
              • S SomeGuyThatIsMe

                you have

                if (!Page.IsPostBack)
                {
                Response.Buffer = true;
                Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
                Response.Expires =-1500;
                Response.CacheControl = "no-cache";

                if (Session["UserName"] != null)
                {
                try
                {
                lblMsg.Text = Session["Type"].ToString()+" ("+ Session["UserName"].ToString()+")";
                }
                catch
                {
                Server.Transfer("../Admin/Default.aspx");
                }
                }
                else
                {
                Server.Transfer("../Admin/Default.aspx");
                }
                }

                correct? what you might try is

                Response.Buffer = true;
                Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
                Response.Expires =-1500;
                Response.CacheControl = "no-cache";

                if (Session["UserName"] != null && Session["Type"] != null)
                {
                lblMsg.Text = Session["Type"].ToString()+" ("+ Session["UserName"].ToString()+")";
                }
                else
                {
                Response.Redirect("../Admin/Default.aspx");
                }

                you only need to have type in the if statement if there is a chance it could be null whitout the UserName bieng null. notice that there is no if(!Page.IsPostBack) statement, this way the code will run EVERY postback, if the session times out and the user tries something they will be redirected to the login(i assume default.aspx is your login page, if its in the same directory as the current page you dont need the relative path just Default.aspx) the nocache options are always set so the back button is effectivly broken(provided they work correctly). this should work, but if the nocache part doesnt, try using meta tags instead.

                Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

                N Offline
                N Offline
                NET India
                wrote on last edited by
                #7

                Same probs But if i put the code given below is working fine in Internet Explorer but not in Flock and Netscape <%@ OutputCache Location="none" %> Now please suggest wht's the problem

                S 1 Reply Last reply
                0
                • N NET India

                  Same probs But if i put the code given below is working fine in Internet Explorer but not in Flock and Netscape <%@ OutputCache Location="none" %> Now please suggest wht's the problem

                  S Offline
                  S Offline
                  SomeGuyThatIsMe
                  wrote on last edited by
                  #8

                  i dotn respond well to orders, when i'm trying to help someone regarless of how politely they're given. i've suggested that if changing values in the response object doesnt work to try meta tags, its what i use and they work perfectly, any decent developer would have tried the suggestion or at least mentioned trying it and having problems if it didnt work, i , for the last time, recomend you do a google search about disabling page cashing and try out what you find which should include several options that work in several browsers.

                  Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

                  1 Reply Last reply
                  0
                  • N NET India

                    Hello All, How to avoid page being cached. Actually i'm creating an Admin Panel so as i click on Log Out button it brings me over the Default page for Log-in again. But the back button of the browser still working and i want to stop it. For it i'm using the following code. But page is still being cached. Response.Buffer = true; Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d); Response.Expires =-1500; Response.CacheControl = "no-cache";

                    D Offline
                    D Offline
                    darkcalin
                    wrote on last edited by
                    #9

                    Try this in page header:

                    N 1 Reply Last reply
                    0
                    • D darkcalin

                      Try this in page header:

                      N Offline
                      N Offline
                      NET India
                      wrote on last edited by
                      #10

                      where is to put this line of code u've given Actually i'm using Master Page and Content Page so should i keep this line of code in Master Page if yes i've done it but same output getting working in Internet Explorer but not in Flock and Netscape Navigator

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups