精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
Questions regarding databinding, in one form or another, are probably the most asked in the ASP.NET newsgroups. It's clear everyone loves the idea of databinding but that more advanced functionality, such as event handling, conditional formatting and fine-tuning, aren't straightforward. The goal of this tutorial is to shed light on some of the more common and frequently asked questions about the capabilities of databinding.ASP.NET里数据绑定方面的问题多。它受欢迎,但是更高级的功能,比如事件处理,条件格式化和微调并不直观。本文对常见问题进行了解释。
The Sample Program
Throughout this tutorial, we'll use two separate data sources. The first will be your every-day DataSet, the other will be a strongly-typed custom collection containing strongly-typed objects.本文里使用了2个独立的数据源。第一个是常用的DataSet,另外一个是包含有强类型对象的强类型定制集合。
Our DataSet will contain two tables, Customers and Orders:我们的DataSet会包含2个表,Customers 和Orders
Customer Structure |
Order Structure |
|||||
Name |
Type |
Description |
Name |
Type |
Description |
|
CustomerId1 |
Int32 |
Unique customer identifier |
OrderId |
Int32 |
Unique order identifier |
|
Name |
String |
Name of the customer |
CustomerId1 |
Int32 |
Identifier of the customer who placed the order |
|
Zip |
String |
Customer's primary ZIP or Portal code |
Ordered |
DateTime |
Date the order was placed on |
|
Enabled |
Boolean |
Whether the customer is currently active/enabled |
Amount |
Decimal |
Dollar value of the order |
1A DataRelation exists between the Customer.CustomerId and Order.CustomerId columns. Customer.CustomerId 和 Order.CustomerId columns之间有关系。
Our business entities will consist of an Owner and a Pet class:商业实体对象由Owner 和 Pet类组成:
Owner Structure |
Pets Structure |
|||||
Name |
Type |
Description |
Name |
Type |
Description |
|
OwnerId |
Int32 |
Unique owner identifier |
PetId |
Int32 |
Unique pet identifier |
|
YearOfBirth |
Int32 |
The year the owner was born in |
Name |
String |
Name of the pet |
|
FirstName |
String |
Owner's first name |
IsNeutured |
Boolean |
Whether or not the pet is neutered |
|
LastName |
String |
Owner's last name |
Type |
PetType |
Indicates the type of pet (Dog, Cat, Fish, Bird, Rodent, Other) |
|
Pets |
PetCollection |
Collection of pets the owner has |
Understanding DataItem理解DataItem
You've undoubtedly made frequent use of the DataItem property, namely when using the DataBinding syntax, to output a value:常用DataItem属性来输出值,这是DataBinding语法的体现。
1: <%# DataBinder.Eval(Container.DataItem, "customerId") %>
It's important to understand that DataItem is actually an object, and that when you use the DataBinder.Eval function, it basically needs to figure out what type of object it is and how to get "customerId" from it. That's because your data source can be different things, such as a DataSet or DataView, an ArrayList or HashTable, a custom collection, and more. Binding happens on a row-by-row basis, and DataItem actually represents the current row being bound. For a DataSet, DataTable, or DataView, DataItem is actually an instance of DataRowView. (You might think that the DataItem for a DataSet or DataTable would be an instance of DataRow, but when you bind either of these, the DefaultView is actually used, therefore DataItem will always be a DataRowView.) When you are binding to a collection, DataItem is an instance of the item within the collection. We can observe this more clearly with the following code:要理解DataItem是个对象,基本上DataBinder.Eval函数需要估计出对象类型和怎样得到”customerId”。这也是为什么你的数据源可以变化的原因,比如可用DataSet也可用DataView,也可是ArrayList或HashTable,更可以是定制集合,也可以是其它更多情况。绑定以行行数据为基本处理形式,DataItem实际上代表了绑定上的当前行。对于DataSet、DataTable和DataView,DataItem实际上是DataRowView的实例。(你可能认为,在服务于DataSet或DataTable时,DataItem会是DataRow的实例,但是绑定上时,实际用的是DefaultView,这样DataItem会总是一个DataRowView)当你用集合时,DataItem是集合的项。如下的代码对此规则有详细的解说:
1: <%@ Import namespace="System.Data" %>
2: <%@ Import namespace="BindingSample" %>
3: <asp:Repeater id="dataSetRepeater" Runat="server">
4: <ItemTemplate>
5: <%# ((DataRowView)Container.DataItem)["customerId"] %> -
6: <%# ((DataRowView)Container.DataItem)["Name"] %> <br />
7: </ItemTemplate>
8: <AlternatingItemTemplate>
9: <%# DataBinder.Eval(Container.DataItem, "customerId") %> -
10: <%# DataBinder.Eval(Container.DataItem, "Name") %> <br />
11: </AlternatingItemTemplate>
12: </asp:Repeater>
13:
14: <br><br>
15:
16: <asp:Repeater id="collectionRepeater" Runat="server">
17: <ItemTemplate>
18: <%# ((Owner)Container.DataItem).OwnerId %> -
19: <%# ((Owner)Container.DataItem).FirstName %> <br />
20: </ItemTemplate>
21: <AlternatingItemTemplate>
22: <%# DataBinder.Eval(Container.DataItem, "OwnerId") %> -
23: <%# DataBinder.Eval(Container.DataItem, "FirstName") %> <br />
24: </AlternatingItemTemplate>
25: </asp:Repeater>
In the first Repeater, we are binding to a DataSet, the ItemTemplate shows how to access values by casting DataItem to a DataRowView [5, 6], the AlternateItemTemplate will output the same information but through DataBinder.Eval [9, 10].在第一个Repeater,我们绑定到DataSet,ItemTemplate里把DataItem转换到DataRowView [5, 6],AlternateItemTemplate通过DataBinder.Eval [9, 10]输出同样的信息。
In the second Repeater, we bind to a custom collection. Again, the ItemTemplate shows how to cast DataItem to the right type and access the fields directly [18, 19] while the AlternateItemTemplate shows how the same is accomplished with