• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

ASP.NET, clear session variable when leaving page

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    ASP.NET, clear session variable when leaving page

    I wonder if any of you .NET experts could offer some guidance with this ?

    I have an asp.net page which uses a session state variable to hold some data. I have a datagrid to which I have bound the results of a database query. The DataView that is bound to the datagrid is saved in the session state so that I can offer column sorting, paging etc without re-querying the database each time.

    It works great (at last - custom paging took some doing but thats another story)

    My question is this - how do I clear this particular session variable when the user navigates to another page on the same site/application (which wont need the DataView, and even if it did, it would be the wrong data)

    Any help with this would be very much appreciated,

    Nathan

    #2
    don't think you want to use session for that. use a protected member var which encaps viewstate...

    Comment


      #3
      Session.Remove["keyname"]

      or

      Session.RemoveAll() to clear all session data

      Comment


        #4
        Originally posted by DimPrawn
        Session.Remove["keyname"]

        or

        Session.RemoveAll() to clear all session data

        ... but what event would you use to run that?

        Comment


          #5
          Well firstly I never really use in proc session state. It kills scalability and it gives problems with server farms.

          So with SQL Server state sessions, I don't care about old session data, it gets cleared down by a SQL Agent job and consumes no RAM.

          If I were storing data in proc session, I would only store a few bytes , not a whole data view.

          Comment


            #6
            It is definitely possible - ive recently written a .Net 1.1 application framework for a project that allows for mostly wizard style interfaces that amass a large amount of data into Session, often jumping into and back from sub wizards, and sometimes standalone pages that place data in Session.

            Obviously there needed to be a way to tidy up data that was no longer required, as soon as possible....

            However my solution to this might be overkill for you if you only require it for a single page...

            Hint: It includes an 'IWorkflow' interface implemented by a base class that all my pages inherit from, a Property that specifies where in memory data should be stored (in a Hashtable), a test to see if that data is no longer required, LinkButtons and Server.Transfer. If you cast the HttpContext.Current.Handler to IWorkflow and it has a state of 'standalone' (the default), the base page does a Session.Remove (keyName). Otherwise, it has been passed via a Server.Transfer and the Properties of IWorkflow are persisted as long as the user stays within the context of a 'workflow' situation. As soon as they hit a page without a Server.Transfer being executed, the IWorkflow Property is set to standalone - the next time they request ANY page, the area of session previously used to collect data is removed.

            This is in conjunction with Custom Web Controls that persist data to Session rather than ViewState, and do so automatically triggered by 'SelectedIndexChanged' type events, and detecting where to persist their state based on the location specified by the base page.

            It suits the architecture, number of users, and requirements for being able to navigate through a large number of potential routes through the application before persisting data, and automatically tidies up after itself. The developers now just add a new project item based on my 3 templates, and make calls to an API to say 'StartSubSession'. They dont have to do anything to clear that up once they have finished with it.
            Vieze Oude Man

            Comment


              #7
              Originally posted by scotspine
              don't think you want to use session for that. use a protected member var which encaps viewstate...
              Thanks for all the comments - I like the sound of this idea. I can see that the datagrid is in the viewstate (by toggling EnableViewState of my DataGrid, then view resultant HTML source, I can see the hidden field __VIEWSTATE has a lot more data when the datagrid's EnableViewState is set to True)
              So, how do I capture the datagrids DataSource from the viewstate ? Which event do I need to write this into ?

              Sorry if these are basic .NET questions; but 'basic' describes my level of understanding and experience at the moment

              Cheers

              Nathan

              Comment


                #8
                set up something like:


                protected DataSet MyDataSet
                {
                get
                {
                if(ViewState["MyDataSet"] == null)
                {
                return null;
                }
                else
                {
                return (DataSet)ViewState["MyDataSet"];
                }
                }
                set
                {
                ViewState["MyDataSet"] = value;
                }
                }



                pass your dataset into thiis on page load and use it thereafter for paging. test by shutting down sql server while paging...

                Comment


                  #9
                  Thank you very much scotspine - works a charm

                  Comment


                    #10
                    Watch your page size mind if your users are using dialup.

                    Other than that ViewState is a good solution in that the burden of state management is distributed amongst the clients and works well on server farms, but can slow the site as all that ViewState has to be uploaded to the webserver on every postback, and most up links are low bandwidth and hence slow.

                    Comment

                    Working...
                    X