更新时间:2023-01-15 08:28:21
使用字典来绑定
You can use a dictionary to bind
<asp:datalist id="datalist" runat="server" datakeyfield="Key" xmlns:asp="#unknown">
<itemtemplate>
<asp:label runat="server" text="<%# Eval("Value") %>"></asp:label>
</itemtemplate>
</asp:datalist>
代码
Code
Dictionary<string,> dictionary = new Dictionary<string,>()
{
{"a", "aaa"},
{"b", "bbb"},
{"c", "ccc"}
};
datalist.DataSource = dictionary;
datalist.DataBind();
您可以使用实现IDictionary和事件DataSet,DataTable的任何集合对象,自定义对象,字典或对象.
看一下链接,这将使您对绑定有一个清晰的认识
详细的数据绑定教程 [
You can use any collection object, custom object, dictionary or object that implements IDictionary and event DataSet, DataTable.
Have a look into the link which will give you fair idea on bindings
A Detailed Data Binding Tutorial[^]
cheers
正如Sandip所说,您可以使用任何IDictionary,数组,集合等.实现要在DataList或DataGrid中填充的IEnumerable接口的任何内容.
您可以使用泛型,IDictionary,IList,HashTables,IList< t> ;,列表无穷无尽.
如果您创建实现通用接口的对象层次结构,例如IMyObject和此接口将属性Value定义为String,将Key定义为String等,可以在List< imyobject>中返回实现此接口的所有对象,然后在DataList UI控件中填充此列表.
问候
Hi,
As Sandip said, you can use any IDictionary, array, collections, etc; anything that implements the IEnumerable interface to be populated in a DataList or DataGrid.
You can use Generics, IDictionary, IList, HashTables, IList<t>, the list is endless..
If you create a hierarchy of Objects that implements a common interface e.g. IMyObject and this interface defines the property Value as String, Key as String, etc., all objects that implement this interface can by returned in a List<imyobject>, and then populate this list in your DataList UI control.
Regards,