开发者

Binding a custom class as a DataSource

开发者 https://www.devze.com 2023-01-29 18:48 出处:网络
Say I have this class: public class Student { private string _name; private int _id; public string Name {

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 IDa开发者_如何学GotaSource.

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.


Create a List Object, Something like

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

FormView1.DataSource = lstStudent;


Bind:

new object[] { student }

instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消