如何绑定一个ASP.NET的FormView到一个记录? [英] How do I bind an ASP.NET FormView to a single record?
问题描述
在ASP.NET FormView控件的全部目的是在一次显示一个记录。那么,如何把它绑定到一个单一的记录?它抱怨
数据源的类型无效。它必须是一个IListSource,IEnumerable的,或者的IDataSource。
块引用>我可以换我的对象列表中,如果这是唯一的解决方案。在这种应用中,FormView控件是一个用户控件中,我检索了公共方法的对象,即我不使用数据源控件。我没有做任何的2路数据绑定在这里无论是,我只使用FormView控件在这种情况下保持一致的外观。
如果我去的书并绑定一个FormView到返回的记录列表的数据源,它实际上检索的所有记录,只是只显示所选择的记录?
下面是我结束了在一个工具类实现:
公共静态的IEnumerable< T> WrapInEnumerable< T>(T项)
{
返回新的T [] {}项;
}
解决方案随着错误消息指出,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)
这篇关于如何绑定一个ASP.NET的FormView到一个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!