Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a custom class as a DataSource

Tags:

.net

asp.net

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 bind it to, 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 I have to implement iListSource, iEnumerable or IDataSource.

I don't know if IListSource and IEnumerable is applicable, and I can't find a good example on how to implement IDataSource.

This is for asp.net.

like image 553
elty123 Avatar asked Apr 27 '26 08:04

elty123


1 Answers

Create a List Object, Something like

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

FormView1.DataSource = lstStudent;
like image 52
Kunal Avatar answered Apr 29 '26 21:04

Kunal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!