Results 1 to 3 of 3

Thread: Variable Persistence in ASP.NET - or lack thereof.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    99

    Variable Persistence in ASP.NET - or lack thereof.

    Inside the class of my webform, I define a new variable, an integer, like this:

    Code:
    Public Class WebForm1
        Inherits System.Web.UI.Page
        Protected test As integer
    Then I have a button that just assigns a value to Test.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            test = 5
        End Sub
    However, when the users clicks a submit button (an empty one) and the page refreshes, test returns to 0. Why? Is there any way I can have this variable persist!?
    ___________________________
    Chris

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    that is what session variables and cache object are for
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Junior Member
    Join Date
    Sep 2002
    Posts
    23
    Use Page.ViewState or Me.ViewState which uses a StateBag object. You use it just like the VB6 Dictionary object.

    Save it before leaving the page.
    Page.ViewState("mypersistedvalue") = Cstr(test)

    On IsPostBack
    Try
    test = CInt(Page.ViewState("mypersistedvalue") )
    Catch
    test = 0
    End Try

    You can persist whole arrays also.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width