Reputation: 334
I have stored a bunch of text values in an array which is randomised. I dynamically create (in VB) a checkboxList then add items to this list, the values of the items are retreived from the randomised array. The checkboxList is then added to a placeHolder on the page. I create the checkboxList and listItems dynamically via onPage Init.
The problem is have is that when you submit the page, you need to re-create the checkboxList again because the page loses the dynamically created content, however because the array is randomised, the next time the elements are re-created, ASP returns the wrong listItems that were selected. For example:
Array is: {1, 2, 3 , 4]
After randomisation: [3,4,2,1]
Page creates checkbox list on page as:
|--| 3
|--| 4
|--| 2
|--| 1
Now i select value 3 and press the submit button.
After submit the checkbox list is re-created using the same function (with randomisation) and thus the checkbox list becomes:
|--| 2
|--| 1
|--| 4
|--| 3
ASP thinks that the selected element that was submitted was actually the first value (which was previously 3 but is now 2), so it tells me that the submit produced the value 2 which in incorrect.
Any ideas on what the best way to get around this would be?
Thank you.
Upvotes: 0
Views: 919
Reputation: 34846
Only randomize the list when the page initially loads (Page_Load
event) and store the order of the array in Session
cache, like this:
If Not Page.IsPostBack Then
' Put randomize logic here
' Remember the order by placing the order in Session
Session("ArrayForOrder") = theArray
End If
As for rebuilding your dynamic content, put that in an Else
block, like this:
If Not Page.IsPostBack Then
' Put randomize logic here
Else
' Put logic here to get the array out from Session to figure out the order
theArray = CType(Session("ArrayForOrder"), Integer())
End If
Upvotes: 0