?
Solved

Persisting an object across multiple postbacks

Posted on 2005-05-15
9
Medium Priority
?
249 Views
Last Modified: 2010-04-07
I'm contructing a Class within my page who members will be built up across multiple postbacks. My first postback returns a dataset that will be used in all subsquent postbacks.

Performance issues of Session vars aside, I will want to save my object to a session var prior to each postback and re-serialize it on return, won't I? When I try to implement as below I get a Session State error immediately on page entry. But other pages have been using session without problems. I understand that the code is trying to access the session state prior to it's availabilty which makes me question my whole approach.

I've used the MyClass stratagy on a number of other pages and am somewhat comfortable with it but this page is made more complex due to the multiple postbacks. I'm trying to resist the urge to just dump individual values out to session vars but ...[gasp & gag].

Exception Details: System.Web.HttpException: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive

Something like:
Public Class MyPage
    Inherits System.Web.UI.Page
  .
  .
  .
  Class MyClass
    [properties]
    [methods]
  End Class
'crux of the problem here.
  Public myContent As MyPage.MyClass = Session("MyContent")
'How can i instantiate the object so all methods can access it but still
'persist it across postback?

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Not IsPostBack) Then
        .
        .
        .

        Else
        .
        .
        .
      End If
    End Sub
   [other stuff]
End Class
0
Comment
Question by:juststeve
9 Comments
 
LVL 3

Expert Comment

by:SilentBob42
ID: 14007601
If other pages can use the Session object, the error message is quite vague I must say. It suggests you have turned off Session State entirely, but you obviously haven't.

So, it looks like the session object does not exists when you try to initialize your private variable. You should define your variable like you did but set it to Nothing initially. Then override a page event (like Page_Load), in which you can store your Session variable.
0
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 14007651
Seems session is not enabled on this page. Open the page in Visual studio and see the page properties:
Make sure "enableSessionState" property is true.


Cheers,
S Sansanwal
0
 

Author Comment

by:juststeve
ID: 14007715
Googling the error text led me here:

http://www.kdkeys.net/forums/376/ShowPost.aspx

Trying to reference System.Web.HttpContext.Current.Session instead of session didn't help but I'm still betting it has something to do with page lifecycle because session stuff works on the page if I reference the session var in the Page_Load event.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 26

Accepted Solution

by:
Rejojohny earned 2000 total points
ID: 14008270
try this ..
Public Class MyPage
    Inherits System.Web.UI.Page
  .
  Class MyClass
    [properties]
    [methods]
  End Class
  Public myContent As new MyPage.MyClass

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Not IsPostBack) Then
       .
       .
       .

        Else
             MyPage.MyClass = Session("MyContent")
       .
       .
     End If
    End Sub
   [other stuff]
End Class

0
 

Author Comment

by:juststeve
ID: 14009754
Thx Rejojohny...that worked. I have a lingering problem remaining due to structuring as MyClass inside the page. How do I get class-based code to correctly reference Page-based controls?

e.g. Outside my class I've declared  Shared lb_AnswersHead As System.Web.UI.WebControls.Label

Inside my class I'm trying to address that control but I get an Object reference not set to an instance of an object:

            Try
                cn.Open()
                dr = SqlHelper.ExecuteReader(cn, CommandType.StoredProcedure, "_Admin_AddQuestion", myParams)
                dr.Read()

            Catch ex As Exception
                lb_QuestionHead.Text = "AN ERROR .."
                lb_QuestionHead.Text = +ex.Message
            Finally
                cn.Close()
            End Try

0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 14009992
u could pass the page as a object reference to the class .. maybe in the constructor of the class ... then i think u should be able to use Mypage.lb_QuestionHead.text or Mypage.findcontrols("lb_QuestionHead") ...
0
 

Author Comment

by:juststeve
ID: 14010488

Thankx...i've tried a couple variations of the above without finding the right syntax. Could you flesh it out a bit?

In javascript we can climb upward by using parent.lb_QuestionHead.Text. Anything similar here?

many thx
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 14010534
am not very good at this .. maybe u could post another question and someone else will help u out ... did the findcontrol also not work?? did u paass the page object to the class?
0
 

Author Comment

by:juststeve
ID: 14010793
good idea...i appreciate the input on the orignal question...it got me over the hump.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question