Asp.net|asp.net begin:
Mastering asp.net DataBinding
Karl Seguin? karlseguin@hotmail.com
Table of Contents
Introduction
The Sample program
Understanding DataItem
Formatting
Inline
Onitemdatabound
onitemcreated
Nested Binding
Inline
Onitemdatabound
Handling Events
Download
This article was available at Code Project. Check it out to make comments, discuss or rate the article
I ' d like to thank Jean-claude Manoli for Developing's his C # Code format, which I used in writing this tutorial.
Introduction
Questions regarding databinding, in one form or another, are probably the most asked in the ASPNET newsgroups. Its clear everyone loves "idea of databinding but" more advanced functionality, such as event handling, conditional formatting and fine-tuning, aren ' t straightforward. The goal of this tutorial are shed light on some to the more common and frequently asked questions about the capabilities O F DataBinding.
The Sample program
Throughout this tutorial we'll use two separate data sources. The Your Every-day dataset, the other would be a strongly-typed custom collection containing strongly-typed o Bjects.
Our dataset would contain two tables, Customers and 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 custom who placed the order
Zip String Customer ' s primary ZIP or Portal code Ordered DateTime Date The order is placed on
Enabled Boolean Whether The customer is currently active/enabled Amount Decimal Dollar value of the order
1 A DataRelation exists between the Customer.customerid and Order.customerid columns.
Our business entities would consist of an Owner and a Pet class:
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 is born in name String name of the pet
FirstName String Owner ' s-A-name isneutured Boolean Whether or not the pet is neutured
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
You have ' ve undoudbtedly made frequent use of the DataItem property, namely if using the DataBinding syntax to output a value :
1: <%# DataBinder.Eval (Container.DataItem, "customerId")%>
It's important to understand so DataItem is actually a object, and that, the DataBinder.Eval function, it b Asically needs to figure out what type of object it are and how to get "customerId" from it. That ' s because your DataSource can is 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 be actually a instance of DataRowView (you might the The DataItem fo R a DataSet or DataTable would be a instance of DataRow, but as you bind either of, and the DefaultView is actually u SED, therefore DataItem would always be a datarowview). When you are binding to a collection, DataItem are an instance the item within the collection. We can observe this more clearly with the following code:
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")%>-
Ten: <%# DataBinder.Eval (Container.DataItem, "Name")%> <br/>
One: </AlternatingItemTemplate>
</asp:Repeater>
13:
: <br><br>
15:
: <asp:repeater id= "Collectionrepeater" runat= "Server" >
: <ItemTemplate>
<%# (Owner) container.dataitem). ownerID%>-
: <%# (Owner) container.dataitem). FirstName%> <