<br/>
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSourceID="ObjectDataSource1"runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetProducts"TypeName="ProductsBLL">
</asp:ObjectDataSource>
Note:
RecallthatwhenbindingadatasourcetoaFormViewcontrolthroughtheFormView’ssmart tag,Visual
Studiocreatedan
ItemTemplate
,
InsertItemTemplate
,and
EditItemTemplate
.WiththeDataList,however,
onlyan
ItemTemplate
iscreated.ThisisbecausetheDataListdoesnothavethesamebuiltineditingandinserting
supportofferedbytheFormView.TheDataListdoescontainedit anddeleterelatedevents,andeditingand
deletingsupportcanbeaddedwithabitofcode,butthere’snosimpleoutoftheboxsupportaswiththe
FormView.We’llseehowtoincludeeditinganddeletingsupportwiththeDataListinafuturetutorial.
Let’stakeamomenttoimprovetheappearanceofthistemplate.Ratherthandisplayingallofthedatafields,let’s
onlydisplaytheproduct’sname,supplier,category,quantity perunit,andunitprice.Moreover,let’sdisplaythe
nameinan
<h4>
headingandlayouttheremainingfieldsusinga
<table>
beneaththeheading.
TomakethesechangesyoucaneitherusethetemplateeditingfeaturesintheDesigner — fromtheDataList’s
smarttagclickontheEditTemplateslink— oryoucanmodifythetemplatemanuallythroughthepage’s
declarativesyntax.IfyouusetheEditTemplatesoptionintheDesigner,yourresultingmarkupmaynotmatchthe
followingmarkupexactly,butwhenviewedthroughabrowsershouldlookverysimilartothescreenshotshownin
Figure8.
<asp:DataListID="DataList1"runat="server"DataKeyField="ProductID"
DataSourceID="ObjectDataSource1"EnableViewState="False">
<ItemTemplate>
<h4><asp:LabelID="ProductNameLabel"runat="server"
Text='<%#Eval("ProductName")%>'/></h4>
<tableborder="0">
<tr>
<tdclass="ProductPropertyLabel">Category:</td>
<td><asp:LabelID="CategoryNameLabel"runat="server"
Text='<%#Eval("CategoryName")%>'/></td>
<tdclass="ProductPropertyLabel">Supplier:</td>
<td><asp:LabelID="SupplierNameLabel"runat="server"
Text='<%#Eval("SupplierName")%>'/></td>
</tr>
<tr>
<tdclass="ProductPropertyLabel">Qty/Unit:</td>
<td><asp:LabelID="QuantityPerUnitLabel"runat="server"
Text='<%#Eval("QuantityPerUnit")%>'/></td>
<tdclass="ProductPropertyLabel">Price:</td>
<td><asp:LabelID="UnitPriceLabel"runat="server"
Text='<%#Eval("UnitPrice","{0:C}")%>'/></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Note:
TheexampleaboveusesLabelWebcontrolswhose
Text
propertyisassignedthevalueofthedatabinding
syntax.Alternatively,wecouldhaveomittedtheLabelsaltogether,typinginjustthedatabindingsyntax.Thatis,
insteadofusing
<asp:LabelID="CategoryNameLabel"runat="server"Text='<%#Eval("CategoryName")
%>'/>
wecouldhaveinsteadusedthedeclarativesyntax
<%#Eval("CategoryName")%>
.
LeavingintheLabelWebcontrols,however,offertwoadvantages.First,itprovidesaneasiermeansforformatting
thedatabasedonthedata,aswe’llseeinthenexttutorial.Second,theEditTemplatesoptionintheDesigner
doesn’tdisplaydeclarativedatabindingsyntaxtha tappearsouts ideofsomeWebcontrol.Instead,theEdit
7 of 19