I have created a class that can contain several instances of an object, all of the data is stored in the session. I won’t know how many instances until run-time. What is the best approach for displaying this dynamic data. Im using aspx with code behind, so I assume it needs to happen in the load sub.
In case it helps, heres the class, its in VB but answers in c# are fine:
Imports System.Web.HttpContext
Public Class Student
Public Property SchoolId As Integer
Public Property Grade As Integer
Public Property StudentName As String
Public Sub AttachToSession(StudentToBeAdded As Student)
Dim StudentList As New List(Of Student)
If (Current.Session("student") Is Nothing) Then
StudentList.Add(StudentToBeAdded)
Current.Session("student") = StudentList
Else
StudentList = Current.Session("student")
StudentList.Add(StudentToBeAdded)
Current.Session("student") = StudentList
End If
End Sub
End Class
Or, if you want a custom look and feel, you could try something like this (in your aspx page):
This would create one ‘div’ element for each item in your student collection.
See: Introduction to ASP.NET inline expressions in the .NET Framework
You might be better off creating a shared property in your student class though:
Then your aspx would be: