DataList Control - Bring on the DataList
(Page 2 of 4 )
Now we get to the meat -- the actual implementation of the DataList. I will first show it in its most basic form, just using the required ItemTemplate and embedding some simple HTML.
<form runat="server">
<asp:DataList id="dList" runat="server" RepeatColumns="3"
RepeatDirection="Horizontal">
<ItemTemplate>
Desc: <%# Container.DataItem("description") %>
<br />
Mfg: <%# Container.DataItem("manufacturer") %>
<br />
Color: <%# Container.DataItem("color") %>
<br />
Cost: <%# FormatCurrency( Container.DataItem("price")) %> Picture:
<br />
<img src='<%# Container.DataItem("imgPath") %>' />
</ItemTemplate>
</asp:DataList>
</form>
Now you'll notice the two things different from a repeater, the RepeatColumns and RepeatDirection properties. What this does in effect is throw the data into an HTML table (unless you modify the RepeatLayout), configured to the number of columns/rows in the direction that you specify. We could have easily added in some quick formatting with the following optional properties:
<asp:DataList id="dList" runat="server" RepeatColumns="3"
RepeatDirection="Horizontal" GridLines="Vertical" BorderWidth="1px" RepeatColumns="3" RepeatDirection="Horizontal" BorderStyle="Solid" BorderColor="#E0E0E0" BackColor="#FFC0C0" ForeColor="#80FF80" >
So at this point you're thinking that the only thing that the DataList really does is save you having to manually include the code to output a repeater into a table. So far you're right, but I haven't gotten into the selecting or editing capabilities. Let's do that now.
Next: Selecting/Editing Templates in a DataList >>
More ASP.NET Articles
More By Justin Cook