Share via


keeping variables alive after postback

Question

Thursday, April 3, 2008 2:32 AM

isn't the page class stays in memory on the server wile the page is displayed ?
i added public variables in the page class and the are empty after postback is done.
What is the way to use to keep them alive ?

All replies (11)

Thursday, April 3, 2008 6:01 AM ✅Answered

i added public variables in the page class and the are empty after postback is done.

 

it is obvious.. and that is what suppose to happen with variables which are declared at Page level .. doesn't matter whether they are private or public...

either you need to declare them as Static / Shared variables (which is not preferable)

or you need to use Session / ViewState Objects ..to use values after postbacks... 

hope it helps./. 


Thursday, April 3, 2008 6:22 AM ✅Answered

The Session Object is the best alternate to share the large dataset among the pages in your case

Storing DataSet in Session:

Session["MyLargeDataSet"] = dsLargeDataSet;

 and at the time of Retrieving from Session you need to do appropriate Cast as.. Session returns type is Object; as..

if(Session["MyLargeDataSet"] != null)
{
    DataSet  dsLargeDataSet = (DataSet)Session["MyLargeDataSet"];
    //...further code..
}
 


Thursday, April 3, 2008 6:47 AM ✅Answered

using session isn't working good for me, because they, for some strange reason, disappears long b4 20 minutes has passed.
and also because it consumes memory.

but thank for all of you for all the help.. [:)]

what work for me was using: "Public Shared ds As New DataSet"  and at the next page "ds = HotelliCalc_CalcResultForm.ds" (thats the NameOfThePage.ds) instead of "PreviousPage.ds"
and also "<%@ PreviousPageType VirtualPath="CalcResultForm.aspx" %>" at the source code of the next page.

(and i did forget to mention that its in VB :) ) tnx again.


Thursday, April 3, 2008 5:34 AM

try making  them static...


Thursday, April 3, 2008 5:35 AM

or save them in hiddenvariables or viewstate.. since http is state less protocol it does happen


Thursday, April 3, 2008 5:55 AM

try making  them static...

when i try, i get : "Static is not valid on a member variables declaration"

and by the way I'm trying to keep alive a large dataset.


Thursday, April 3, 2008 5:58 AM

strore your varible in session like

Session("yourvariable")="test"

response.write(session("yourvariable"))


Thursday, April 3, 2008 6:18 AM

shared worked but only for this page, and cause it is private the next page cant see it when I'm using "PreviousPage.ds" (Shared ds As New DataSet) also tried (Public Shared ds As New DataSet) it creates different problems at the next page


Thursday, April 3, 2008 6:25 AM

 For variables within a page class u can declare shared in vb.net and static in c# and for the values to be transferred accross page u can use session or query string method .


Saturday, October 31, 2009 9:39 PM

I'm having a similar problem (I think).

I'm VERY new to C# (and ASP.NET development in general) and am still learning my way around, so I'm not quite sure if this is a Postback issue or something else (in my case). 

I have one button on the page that searches through a list of directories and returns a List<string> of filenames matching a certain filetype (*.mp3 in this case).  When that event is complete, the page displays another button to submit the displayed values (filenames and paths) to be INSERT'ed into a database.  The variable is always empty and never inserts any records.

I've tried the following:

protected void btnSearch_Click(object sender, Eventargs e) {
     // code to retrieve filenames

     Session["MP3List"] = mp3FileList;
}

protected void btnSubmit_Click(object sender, EventArgs e) {
     if(Session["MP3List"] != null)
     {
          //code to insert into DB
     }
     else
     {
          lblErrorMessage.Text = "Variable was empty.";
     }
}

 

I always get that the variable is empty.  I haven't tried what seemed to work for Yakov yet, but that seems "wrong" to be, being a long-time Perl scripter.  So I guess the question is, is this really a postback event?  If so, why doesn't the suggested Session[] save work?  (I have also tried ViewState[] saves.)

Also, just realized I'm bumping an old thread.  Sorry about that.  Please let me know if you need more detailed code samples.

Thanks.


Sunday, November 1, 2009 10:25 AM

I finally got this working.  :)