开发者

Binding a custom class as a DataSource

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜