我创建了一个类,它可以包含一个对象的多个实例,所有的数据都存储在会话中。直到运行时我才知道有多少实例。显示此动态数据的最佳方法是什么。我使用带有代码的 aspx,所以我认为它需要在加载子中发生。
如果它有帮助,继承类,它在 VB 中,但在 c# 中的答案很好:
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