Solved array doesn't hold values

Posted on 2011-10-22
Last Modified: 2012-06-21

I have declared an arrau at the top of the code behind my webform (see code below)
As I assign different values to the array after my Gridview is declared, the rest of the array values seem to clear. Not sure why this is happening.

Dim arrSuccess(19) As String 'holds successful entry data

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Open in new window

Question by:murbro

    Expert Comment

    Can you provide the complete code?
    LVL 1

    Expert Comment

    Hi murbro,

    An ASP.NET page class file is destroyed after it is served to the client.

    The most chosen method for preserving variables in ASP.NET from one page
    load to the next is to store them in viewstate which is a hidden input:

    ViewState("arrSuccess") = ....

    You could use viewstate or session objects to persist items.

    Session("arrSuccess") = ....

    for more information :

    Author Comment

    No. Sorry. Have deleted it and reverted to using labels to hold my values. Any general information felating to why an array would lose its values on postback would help. Thanks
    LVL 1

    Accepted Solution

    this is the expected behaviour as the pages ar stateless. you need to store that variable in viewstate etc, then nexttime you need to fecth it from viewstate.

    Exemple :

    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
      ' Sample ArrayList for the page.
      Dim arrSuccess As ArrayList
      Function CreateArray() As ArrayList
        ' Create a sample ArrayList.
        Dim result As ArrayList
        result = New ArrayList(4)
        result.Add("item 1")
        result.Add("item 2")
        result.Add("item 3")
        result.Add("item 4")
        Return result
      End Function
      Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If (Me.ViewState("arrayListInViewState") IsNot Nothing) Then
          PageArrayList = CType(Me.ViewState("arrayListInViewState"), ArrayList)
          ' ArrayList isn't in view state, so we need to load it from scratch.
          PageArrayList = CreateArray()
        End If
        ' Code that uses PageArrayList.
      End Sub
      Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        ' Save PageArrayList before the page is rendered.
        Me.ViewState.Add("arrayListInViewState", PageArrayList)
      End Sub

    Open in new window


    Author Closing Comment

    Great. Thank you very much for the help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now