Click to See Complete Forum and Search --> : Variable Scope between Postbacks
TSmooth
August 22nd, 2005, 03:33 PM
How can I go about maintaining the data of a System.Collections.ArrayList object between postbacks?
Currently I have the collections declared in the .aspx's class scope and I have several panels on my page with only one being shown at a time. However, when the "Next" button is clicked on Panel1, and Panel2 is shown, my collection no longer contains any items. What are my options for keeping the data persistent? Do i have to use Session variables?
Thanks for any help,
Tom
aquafin
August 22nd, 2005, 09:13 PM
To keep data Persistent , you can decalre your variables or arrays in Module
For example
Module ArrayFunctions 'To store the Questions, Answers in Arrays
Public ArrQuestions As New ArrayList
Public Sub AddQuestions(ByVal man As String)
ArrQuestions.Add(mine)
End Sub
End Module
sub page_load(....
' call the sub in the module
AddQuestions(Question)
end sub
Make sure you clear all module variables when the application/session ends
TSmooth
August 22nd, 2005, 09:25 PM
Thank you, didn't think about a module for some reason. I did however get it working with the session object but I think I like the module better. I have a different problem now though...
I am dynamically adding a variable amount of combo boxes to a panel during run time. I add a reference to these combo boxes to an ArrayList object. There is also a button on this panel. The ArrayList is being filled properly but when the button is clicked and I process the Button's Command event for the click, no matter what is selected in the combo boxes when the button is clicked the SelectedItem and SelectedValue properties always refer to the first item in the list. For some reason it's resetting these combo boxes. Any ideas why?
aquafin
August 22nd, 2005, 09:42 PM
if you could send me the code , i can help you ,
You might find this thread helpful
http://www.codeguru.com/forum/showthread.php?t=353617
cmiskow
August 23rd, 2005, 09:16 AM
I'm not sure but it seems to me that the module solution will have a global scope, so it is more like using the Application than the Session. Just be sure to test it with multiple concurrent users.
TSmooth
August 23rd, 2005, 09:35 PM
My page consists of about 5 panels that are setup to display like a wizard in that each one has a "Back" and "Next" button which shuffles through panels 1-5 (only one visible at once). In my page_load function, I call a function that adds checkbox controls to panel 2. The number of check boxes is determined by a value input on panel 1. The function to add the checkboxes to the panel is called every time during the page load event (not inside an IsPostBack If statement) and these checkboxes always maintain their viewstate. On panels 3 and 4 however, I add controls dynamically based on data input on panel 2. These controls are added when the next button on panel 2 is clicked. The controls show up fine but as soon as I move onto another panel the values inside these controls is lost and I don't understand why? I need these dynamically created controls to maintain their viewstate even though the panel is no longer visible. What am I doing wrong?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.