Web Studyaspnet.blogspot.com Babyneed.blogspot.com

ASP.Net Web Developer Guide

Power By: eXTReMe Tracker Powered by Blogger  
 
 
 
 

 

 
Important Links
 
   
     
 
Sitemap
 
   
   
 
Reference
 
   

Tuesday, March 28, 2006

Displaying Data in a Repeater Control (Chapter 3)


Suppose that we want to display our products data for the products that cost more than $45.00.The expected display for this application is shown in Figure 3.68.The code for this application is shown in Figure 3.69.

Figure 3.68 Displaying Data in a Repeater Control

In this application we have defined three templates for our repeater.The Header template starts an HTML table with a <table> tag.The Footer template completes the table with a </table> tag.The ItemTemplate contains the table cells to house the data values.We will extract data from the Products table from the Products.mdb database. First we will populate a data set object, and then we will bind the repeater to this data set. Detailed code for populating the data set and binding the repeater is shown in Figure 3.69.

Figure 3.69 Repeater1.aspx
<!— Chapter3/Repeater1.aspx —>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html><head></head>
<script language="VB" Debug="true" runat="server">
Sub Page_Load(src As Object, e As EventArgs)
If Not IsPostBack
bindListControl
End If
End Sub
Sub bindListControl()
Dim myConn As OleDbConnection
Dim myOleDbAdapter As OleDbDataAdapter
Dim connStr, sqlStr As String
Dim myDataSet As New Dataset
connStr="Provider=Microsoft.Jet.OLEDB.4.0;" _
+ "Data Source=D:\Products.mdb"
sqlStr="SELECT ProductId, ProductName, Price, ImagePath " _
+ "FROM Products WHERE Price>45.00 ORDER BY Price"
myConn= New OleDbConnection(connStr)
myConn.Open()
myOleDbAdapter =New OleDbDataAdapter(sqlStr,myConn)
myOleDbAdapter.Fill(myDataSet,"dtProducts")
repeater1.DataSource=myDataSet.Tables("dtProducts")
repeater1.DataBind()
End Sub
</script>
<body><h2><center>Cathy's E-Shop</h2>
<asp:Repeater id="repeater1" runat="server" >
<HeaderTemplate><table></HeaderTemplate>
<ItemTemplate><tr>
<td><asp:Image height=100 width=100
Img src='<%# Container.DataItem("ImagePath")%>'
runat="server"/>
</td>
<td>Product ID:
<%# Container.DataItem("ProductId")%><br>
Description: <b><i>
<%# Container.DataItem("ProductName")%></b><i><br>
<b>Unit Price:
<%# FormatCurrency(Container.DataItem("Price"))%></b><br>
</td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</center></body></html>
Once a data table has been populated, only two statements are required to bind a repeater.We need to set its DataSource property to the appropriate data table, and then we can apply its DataBind() method to accomplish the job.These two statements are as follows:
repeater1.DataSource=myDataSet.Tables("dtProducts").DefaultView
repeater1.DataBind()
We know that the dtProducts table of our data set will contain columns like ProductId, ProductName, etc. Our objective is to develop an ItemTemplate where we want to specify which column should be shown in what format. For each row of the table in the data set, the repeater will employ this template to display the data. A typical way to display a desired field is to use the <%# Container.DataItem(“columnName”)%> syntax. For example, the following ItemTemplate will display the ProductId in a cell of a table (assuming that the <table> tag has been specified in the HeaderTemplate):
<ItemTemplate>
<tr><td><%# Container.DataItem("ProductId") %>
</td></tr>
</ItemTemplate>
Similarly, as shown in the following statement, an Img control can also be specified to render an image:
Img src='<%# Container.DataItem("ImagePath") %>'

0 Comments:

Post a Comment

<< Home