<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Binding a Variable Select Query to a DataList Control ( SQL )</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script runat="server">
protected void Page_Load ( object src, EventArgs e ) {
if ( !IsPostBack ) {
string query = "SELECT DISTINCT Subject FROM Books";
mySelect.DataSource = fetchReader ( query );
mySelect.DataBind ( );
}
}
public void getBooks ( object src, EventArgs e ) {
string query = "SELECT * FROM Books WHERE Subject = '" + mySelect.Value + "' AND Price IS NOT NULL";
myDataList.DataSource = fetchReader ( query );
myDataList.DataBind ( );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Binding a <b>Variable Select Query</b> to a DataList Control</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<center>
<form runat="server">
<p>Select a subject category:
<select id="mySelect" datatextfield="Subject" runat="server" />
<input type=submit value="Get Books" onServerClick="getBooks" runat="server" />
<p>
<asp:datalist id="myDataList" runat="server"
width=90% repeatcolumns=2 repeatdirection="horizontal"
itemstyle-width=50%>
<itemtemplate>
<table width=92% align="center"
cellpadding=10 style="font-size:10pt">
<tr>
<td width=25% valign="top">
<a href='<%# Eval ( "titleid", "details_book.aspx?titleid={0}" ) %>'>
<img src='<%# Eval ( "titleid", "/shared/books/{0}.jpg" ) %>'
style="box-shadow: 3px 3px 3px gray"></a>
</td>
<td width=75% valign="top">
<p style="font:bold 13pt arial">
<a href='<%# Eval ( "titleid", "details_book.aspx?titleid={0}" ) %>'>
<span style="color:darkgreen"><%# Eval ( "title" )%></span></a></p>
<b>Title ID:</b> <%# Eval ( "titleid" ) %><br>
<b>Author:</b> <%# Eval ( "author" ) %><br>
<b>Category:</b> <%# Eval ( "subject" ) %><br>
<b>Publisher:</b> <%# Eval ( "publisher" ) %><br>
<b>Price (from):</b> <%# Eval ( "price", "{0:c2}" ) %><p>
</td></tr>
</table>
</itemtemplate>
</asp:datalist>
</form>
</center>
<% if ( Request.Form [ "mySelect" ] != null ) {%>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
<% } %>
</body>
</html>