Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何显示基于泛型对象的动态html_Asp.net_Vb.net - Fatal编程技术网

Asp.net 如何显示基于泛型对象的动态html

Asp.net 如何显示基于泛型对象的动态html,asp.net,vb.net,Asp.net,Vb.net,我创建了一个类,它可以包含一个对象的多个实例,所有数据都存储在会话中。在运行时之前,我不知道有多少实例。显示此动态数据的最佳方法是什么。我正在使用aspx和代码隐藏,所以我假设它需要在load sub中发生 如果有帮助的话,下面是课程,它是用VB编写的,但c#的答案很好: Imports System.Web.HttpContext Public Class Student Public Property SchoolId As Integer Public Property

我创建了一个类,它可以包含一个对象的多个实例,所有数据都存储在会话中。在运行时之前,我不知道有多少实例。显示此动态数据的最佳方法是什么。我正在使用aspx和代码隐藏,所以我假设它需要在load sub中发生

如果有帮助的话,下面是课程,它是用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
您可以使用控件来显示学生信息

加价

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True">
</asp:GridView>

或者,如果您想要定制的外观和感觉,您可以尝试以下内容(在您的aspx页面中):


姓名:

等级:

这将为学生集合中的每个项目创建一个“div”元素

见:

不过,在学生类中创建共享属性可能会更好:

Public Shared ReadOnly Property Students As List(Of Student)
    Get
        Dim l_studentList As List(Of Student) = TryCast( Current.Session("student"), List(Of Student) )
        If l_studentList Is Nothing Then
            l_studentList = New List(Of Student)
            Current.Session("student") = l_studentList
        End If

        Return l_studentList
    End Get
End Property
那么您的aspx将是:

<% For Each l_student As Student In Student.Students %>
...

...

这个问题非常模糊。你有更具体的问题吗?也许你可以用你已经尝试过的,或者你正在寻找的结果来展示。对不起,我不需要实际的html,只是一种显示动态数据的方式。就像我通常只使用标签或文本框并在服务器上运行一样,但这些控件不会映射到我的数据结构。我在考虑两种可能的方法——一种包含html字符串的标签,或者某种方式的自定义控件。我在.net中不是很先进,所以我不确定这是否会对你的问题有帮助。你应该考虑编辑它。谢谢你的快速反应。另一个答案更接近我想要实现的目标
<% For Each l_student As Student In Student.Students %>
...