且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何绑定一个ASP.NET的FormView到一个记录?

更新时间:2023-01-06 22:43:14

随着错误消息指出,DataSource对象必须实现IListSource,IEnumerable的还是要的IDataSource工作。

如果你有一个对象的AV A型不执行提到的一个接口,那么你可以像你说换你对象的列表:

C#

  VAR myDataSource =新的List< A> {myObject的};

VB.NET

 昏暗myDataSource方式列表(一)(myObject的)

The whole purpose of the ASP.NET FormView control is to display a single record at a time. So how do I bind it to a single record? It complains that

Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

I can wrap my object in a list if that's the only solution. In this application, the FormView is inside a user control and I'm retrieving the object in a public method, i.e. I'm not using a data source control. I'm not doing any 2-way data binding here either, I'm only using the FormView in this case to maintain a consistent look.

If I went by the book and bound a FormView to a data source that returned a list of records, would it actually retrieve all the records and just display only the selected record?

Here's what I ended up implementing in a utility class:

public static IEnumerable<T> WrapInEnumerable<T>(T item)
{
    return new T[] {item};
}

As the error message says, the DataSource object must implement IListSource, IEnumerable or IDataSource to work.

If you have an object av type A which do not implement one of the mentioned interfaces then you could as you say wrap your object in a list:

C#

var myDataSource = new List<A> {myObject};

VB.NET

Dim myDataSource As List(Of A)(myObject)