Binding a custom class as a data source

Let's say I have this class:

public class Student {

    private string _name;
    private int _id;

    public string Name 
    {
    // get and set
    }

    public int ID
    {
    // get and set
    }

}

I want to link it, say FormView

<asp:FormView runat="server" ID="FormView1">
<ItemTemplate>
    <asp:Label runat="server" id="lblName" Text="<% Eval('Name') %>" />
</ItemTemplate>
</asp:FormView>

However, when I try to do

FormView1.DataSource = student;

I will get an error saying that I need to implement iListSource, iEnumerable or IDataSource.

I do not know if IListSource and IEnumerable are applicable, and I cannot find a good example of how to implement IDataSource.

This is for asp.net.

+3
source share
2 answers

Create a list object, Something like

List<Student> lstStudent = new List<Student>();
lstStudent.add(student);

FormView1.DataSource = lstStudent;
+4
source

Bind:

new object[] { student }

instead.

0
source

Source: https://habr.com/ru/post/1779242/


All Articles