views:

33

answers:

2

Hey all, i am new at everything VB.net/ASP.net so i need some help with a problem i am currently having.

I have an ASCX.vb page that lets the user choose someone from a table. Once they do select someone, the page reloads with that persons information into another table below that one where they can submit an order for that person or remove that person.

Problem being is that i wanted to store the users ID that are selected but it seems that everytime the page reloads to show an update, it dim's my array again back to default.

This is my code once they choose a user:

Namespace prog.Controls
  Partial Public Class progReportGrid
  etc....

  Public strIDArray() As String = {"0000770000"} 'Just a dummy place holder

Private Sub gvData_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvData.RowCommand

  Dim idIndexNumber As Integer = Array.IndexOf(strIDArray, strID)

  For i As Integer = 0 To strIDArray.Length - 1
      System.Diagnostics.Debug.WriteLine(strIDArray(i))
  Next

  If idIndexNumber = -1 Then
      ReDim Preserve strIDArray(strIDArray.Length)
      strIDArray(strIDArray.Length) = strID
      RaiseEvent EmployeeSelected(Me, New ESEventArgs(strID))
  End If
End Sub

So everytime to page reloads the Public strIDArray() As String = {"0000770000"} gets called again and, of course, clears anything that was saved to it other than 0000770000.

How can i keep it from doing this?

UPDATE

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
             'do what?
        End If
End Sub

David

+2  A: 

Use the Page.IsPostBack property in your Page_Load method.

This property is false for the first time the page loads and false for every postback afterwards.

If you set your default value strIDArray() = {"0000770000"} within If (Page.IsPostBack <> true) then it will not get reset on the postback.

EDIT : My VB syntax is VERY shaky but let me try it out

Partial Public Class EARNReportGrid 

Begin

Public strIDArray() As String // DO NOT INITIALIZE it here. ONLY DECLARE IT
.....

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
        If Not Page.IsPostBack Then 
             //INITIALIZE the ARRAY
             strIDArray = New String(){"00007700000"}
        End If 
End Sub
......    
End
InSane
Thanks for the reply, In. Check out my updated code above. I already had that at startup but i am unable to have a public array in that...
StealthRT
@StealthRT - code still looks the same
InSane
Check again? its under the **UPDATE** part...
StealthRT
Updated the answer - hope this helps! The declaration of the array should still be where it was earlier. Just the initialization is moved to the NOT Page.IsPostback part. Excuse my VB.net syntax - i thought i still remembered it but my vanity is greater than my memory
InSane
Its returning strIDArray as **nothing** now when it try's to do the **Array.IndexOf(strIDArray, strID)**
StealthRT
Are you not updating the strIDArray with the updated values on postback? The else part of NOT Page.IsPostback should be setting the correct value in the array based on whatever user id was selected when page was first loaded
InSane
When it adds to the array it goes through a different sub. When it gets to checking whats in the array it says theres nothing when there should be **at least** the 00007700000.
StealthRT
When it gets to this part of the code **Dim idIndexNumber As Integer = Array.IndexOf(strIDArray, strID)** thats where it says strIDArray is nothing.
StealthRT
+2  A: 

Perhaps you have a misunderstanding with the stateless model of a web application. Anything that is not stored in the ViewState or Session or a shared application variable will NOT persist through postbacks.

This means that when you declare your Public strIDArray() As String as a standard variable, it will be re initialized every time the page posts back. For example, here's a simple flow of how this works:

  • The user opens their browser and opens up your aspx web page.
  • The request goes to your server, and all the controls including progReportGrid are instantiated as new instances and added to the page. (this is where your variable gets set to its original value every time)
  • A bunch of events are fired, including the Me.Load event
  • Controls that were added to the page are asked to generate their HTML
  • The HTML gathered from all the controls on the page is sent back to the user

So because your controls are all re-instantiated every post back, class variables like strIDArray are pretty much thrown out after the page is sent to the user.

Instead, if you want the page to remember what value the array had last postback and add more to it next postback, you have to use either ViewState, or Session.

For example:

Private Property StrIDArray() As String()
  Get
    If ViewState("StrIDArray") Is Nothing
       ViewState("StrIDArray") = New String() {"0000770000"}
    Return ViewState("StrIDArray")
  End Get
  Set(ByVal value As String())
     ViewState("StrIDArray") = value
  End Set
End Property

Now if you used this property instead of your variable, you could manipulate that array and as long as the user is on that page, the value of that array will persist across postbacks.

+1: although I would add cookies and hidden form fields to the list of places to store things. For this particular item I'd suggest a hidden form field.
Chris Lively