Results 1 to 4 of 4

Thread: ViewState variable not retaining its value

  1. #1

    Thread Starter
    Hyperactive Member Utpal's Avatar
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    393

    ViewState variable not retaining its value

    Hi everybody,

    In my web form, I have declared a form level variable - vabc. I am trying to retain the value of the variable after a postback using ViewState. Following is my code:

    VB Code:
    1. Dim vabc as String
    2.  
    3.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         If Page.IsPostBack Then
    5.             vabc = ViewState("vabc")
    6.         Else
    7.             vabc = "vParty01"
    8.         End If
    9.     End Sub
    10.  
    11.     Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
    12.         ViewState("vabc") = vabc
    13.     End Sub

    However, after a postback the value stored in ViewState("vabc") is lost. Why is it not retaining the value? What is the solution?
    It is easy when you know it.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: ViewState variable not retaining its value

    Page_Unload is not the same in the traditional sense as Windows Forms. The page unload doesn't happen when you close the browser or move to another page. The Page_Unload event happens when the page has finished being processed by ASP.NET, and before it's sent to the browser.

    You need to find some other event to set the ViewState on.

    Keep in mind, ViewState only saves between page postbacks. If you navigate to another page and back it's lost. For that you need a Session variable.

  3. #3

    Thread Starter
    Hyperactive Member Utpal's Avatar
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    393

    Re: ViewState variable not retaining its value

    The Page_Unload event happens when the page has finished being processed by ASP.NET, and before it's sent to the browser.
    Do you mean to say that the ViewState variable should be set after the page has been sent to the browser?
    It is easy when you know it.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: ViewState variable not retaining its value

    Remove the Page_Unload event.

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