ASP.NET Page Variables Persistence

Discussion in 'ASP .Net' started by =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=, Jun 26, 2004.

  1. Hi,

    I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?

    Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?



    Thank you,

    Mark Anthony Spiteri
     
    =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=, Jun 26, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=

    Gareth Jones Guest

    On Sat, 26 Jun 2004 08:08:01 -0700, "Mark Anthony Spiteri"
    <mark@[remove]mspiteri.com> wrote:

    >Hi,
    >
    >I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?
    >
    >Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?


    You'll need to use the ViewState object, or perhaps the Session
    object. A static variable isn't enough - in a web environment the
    application is restarted for every request, so your static variables
    are being reinitialised.

    Gareth
     
    Gareth Jones, Jun 26, 2004
    #2
    1. Advertising

  3. Thank you for your reply.

    "Gareth Jones" wrote:

    > On Sat, 26 Jun 2004 08:08:01 -0700, "Mark Anthony Spiteri"
    > <mark@[remove]mspiteri.com> wrote:
    >
    > >Hi,
    > >
    > >I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?
    > >
    > >Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?

    >
    > You'll need to use the ViewState object, or perhaps the Session
    > object. A static variable isn't enough - in a web environment the
    > application is restarted for every request, so your static variables
    > are being reinitialised.
    >
    > Gareth
    >
     
    =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=, Jun 27, 2004
    #3
  4. Thank you for your reply

    "Gareth Jones" wrote:

    > On Sat, 26 Jun 2004 08:08:01 -0700, "Mark Anthony Spiteri"
    > <mark@[remove]mspiteri.com> wrote:
    >
    > >Hi,
    > >
    > >I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?
    > >
    > >Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?

    >
    > You'll need to use the ViewState object, or perhaps the Session
    > object. A static variable isn't enough - in a web environment the
    > application is restarted for every request, so your static variables
    > are being reinitialised.
    >
    > Gareth
    >
     
    =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=, Jun 27, 2004
    #4
  5. =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=

    Scott Allen Guest

    Are the values of the page global variables supposed to be the same
    for all of the users who visit the page?

    --
    Scott
    http://www.OdeToCode.com

    On Sat, 26 Jun 2004 08:08:01 -0700, "Mark Anthony Spiteri"
    <mark@[remove]mspiteri.com> wrote:

    >Hi,
    >
    >I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?
    >
    >Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?
    >
    >
    >
    >Thank you,
    >
    >Mark Anthony Spiteri
     
    Scott Allen, Jun 27, 2004
    #5
  6. No,

    each user will have different variable values. Thus making use of the Application Object is futile.

    "Scott Allen" wrote:

    > Are the values of the page global variables supposed to be the same
    > for all of the users who visit the page?
    >
    > --
    > Scott
    > http://www.OdeToCode.com
    >
    > On Sat, 26 Jun 2004 08:08:01 -0700, "Mark Anthony Spiteri"
    > <mark@[remove]mspiteri.com> wrote:
    >
    > >Hi,
    > >
    > >I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?
    > >
    > >Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?
    > >
    > >
    > >
    > >Thank you,
    > >
    > >Mark Anthony Spiteri

    >
    >
     
    =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=, Jun 27, 2004
    #6
  7. =?Utf-8?B?TWFyayBBbnRob255IFNwaXRlcmk=?=

    Scott Allen Guest

    Yes, this also rules out using static variables. As suggested, Session
    state and Viewstate are both options.

    --s

    On Sun, 27 Jun 2004 09:05:01 -0700, "Mark Anthony Spiteri"
    <mark@[remove]mspiteri.com> wrote:

    >No,
    >
    >each user will have different variable values. Thus making use of the Application Object is futile.
    >
    >"Scott Allen" wrote:
    >
    >> Are the values of the page global variables supposed to be the same
    >> for all of the users who visit the page?
    >>
    >> --
    >> Scott
    >> http://www.OdeToCode.com
    >>
    >> On Sat, 26 Jun 2004 08:08:01 -0700, "Mark Anthony Spiteri"
    >> <mark@[remove]mspiteri.com> wrote:
    >>
    >> >Hi,
    >> >
    >> >I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?
    >> >
    >> >Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?
    >> >
    >> >
    >> >
    >> >Thank you,
    >> >
    >> >Mark Anthony Spiteri

    >>
    >>


    --
    Scott
    http://www.OdeToCode.com
     
    Scott Allen, Jun 27, 2004
    #7
  8. "Mark Anthony Spiteri" <mark@[remove]mspiteri.com> wrote in message
    news:...
    > Hi,
    >
    > I am designing an ASP.NET application in which particular pages require

    global variables that need to be persisted during postbacks. What is the
    best practice to do this?
    >
    > Is it to use the viewstate object or is declaring the variables as static

    sufficient (I am currently using this method)?

    Use ViewState if you need the variables to persist for multiple postbacks to
    the same page. Use Session state if you need longer persistence.

    Using static variables is almost always wrong, as they will be shared
    amongst users. They will also have to be synchronized if written to because
    multiple requests may access them at the same time.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jun 27, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Mauldin
    Replies:
    7
    Views:
    498
    Steve Mauldin
    Jan 23, 2006
  2. Scott Yanoff
    Replies:
    0
    Views:
    519
    Scott Yanoff
    Aug 11, 2003
  3. javaguy44
    Replies:
    10
    Views:
    987
    Michael Berg
    May 18, 2004
  4. Kenneth P. Turvey

    Java Persistence API and persistence.xml

    Kenneth P. Turvey, Mar 15, 2008, in forum: Java
    Replies:
    2
    Views:
    17,343
    Kenneth P. Turvey
    Mar 16, 2008
  5. Nadav

    Custom Controls Variables persistence.

    Nadav, Dec 31, 2003, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    181
    Jeffrey Tan[MSFT]
    Jan 2, 2004
Loading...

Share This Page