简体   繁体   English

在ASP.NET中访问类属性

[英]Accessing Class Properties in ASP.NET

I have 2 C# classes: 我有2个C#类:

public class Course
{
    public string Title{get;set;}
    public int Code {get;set;}
}

public class Student 
{
    public int Id{get;set;}
    public string Surname{get;set;}
    public Course Course {get;set}
...
}

As you can see the Student class contains a reference to Course. 如您所见,Student类包含对Course的引用。 I have a method in my application to return me a student object. 我的应用程序中有一个方法可以向我返回一个学生对象。 In my ASP.NET .aspx file I have the following code: 在我的ASP.NET .aspx文件中,我具有以下代码:

...

<columns>
<asp:BoundField DataField="Id" HeaderText="Id"/>
<asp:BoundField DataField="Surname" HeaderText="Surname"/>
...
</columns>

...

How do I access the values of the Course property in the Student class (ie how do I get the course code and title for the current student). 如何访问Student类中Course属性的值(即,如何获取当前学生的课程代码和标题)。

You may find the following blog post useful. 您可能会发现以下博客文章很有用。 Or as an alternative you could use a TemplateField: 或者,您也可以使用TemplateField:

<asp:TemplateField HeaderText="Course Title">
    <ItemTemplate>
        <%# Eval("Course.Title") %>
    </ItemTemplate>
</asp:TemplateField>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM