<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<title>Binding a DataReader to a DataList Control</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="~/shared/fetchData_sql.cs" />
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
// specify the data source
SqlConnection myConn = new SqlConnection ( getConnection ( "aspnet" ) );
// define the command query
SqlCommand myCmd = new SqlCommand ( "SELECT * FROM titles", myConn );
// open the connection
myConn.Open ( );
// bind the datalist to the reader
myList.DataSource = myCmd.ExecuteReader ( CommandBehavior.CloseConnection );
myList.DataBind ( );
// close the connection
myConn.Close ( );
}
</script>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Binding a <span class="hilite">DataReader</span> to a <span class="hilite">DataList</span> Control</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<p align="center">
<form runat="server">
<asp:datalist id="myList" runat="server"
width=90% repeatcolumns=2
itemstyle-width=50%>
<itemtemplate>
<table cellpadding=10>
<tr>
<td valign="top">
<a href='<%# Eval ( "title_id", "details_title.aspx?titleid={0}" ) %>'>
<img alt="Click for details" border=0 runat="server"
src='<%# Eval ( "title_id", "~/shared/images/title-{0}.gif" ) %>'
style="box-shadow: 3px 3px 3px gray" /></a>
</td>
<td valign="top">
<b>Title:</b> <a href='<%# Eval ( "title_id", "details_title.aspx?titleid={0}" ) %>'>
<%# Eval ( "title" ) %></a><br>
<b>Category:</b> <%# Eval ( "type" ) %><br>
<b>Publisher ID:</b> <%# Eval ( "pub_id" ) %><br>
<b>Price:</b> <%# Eval ( "Price", "{0:c}" ) %>
</td></tr>
</table>
</itemtemplate>
</asp:datalist>
</form>
</p>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>