user762342
user762342

Reputation: 5

Accessing Class Properties in ASP.NET

I have 2 C# classes:

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. I have a method in my application to return me a student object. In my ASP.NET .aspx file I have the following code:

...

<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 (i.e. how do I get the course code and title for the current student).

Upvotes: 1

Views: 1234

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

You may find the following blog post useful. Or as an alternative you could use a TemplateField:

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

Upvotes: 3

Related Questions