<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>Populating an HtmlSelect Control from a Database</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style>
.orderID {font:bold italic arial; color:darkcyan}
</style>
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs E ) {
OleDbConnection myConnection = new OleDbConnection (
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
Server.MapPath ( "~/app_data/dbtutor.mdb" ) );
if ( !IsPostBack ) {
OleDbDataAdapter myAdapter = new OleDbDataAdapter ( "SELECT DISTINCT ProductType FROM Products", myConnection );
DataSet myDataSet = new DataSet ( );
myAdapter.Fill ( myDataSet, "Types" );
mySelect.DataSource = myDataSet.Tables [ "Types" ].DefaultView;
mySelect.DataBind ( );
}
}
public void getProducts ( object src, EventArgs E ) {
OleDbConnection myConnection = new OleDbConnection (
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
Server.MapPath ( "~/app_data/dbtutor.mdb" ) );
string selectCmd = "SELECT ProductType, ProductCode, ProductName, ProductDescription, ProductImageURL, UnitPrice FROM Products WHERE ProductType='" + mySelect.Value + "'";
OleDbDataAdapter myAdapter = new OleDbDataAdapter ( selectCmd, myConnection );
DataSet myDataSet = new DataSet ( );
myAdapter.Fill ( myDataSet, "Products" );
myDataList.DataSource = myDataSet.Tables [ "Products" ].DefaultView;
myDataList.DataBind ( );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Populating an <span class="hilite">HtmlSelect Control</span> from a Database</h2></div>
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<p>Select Gear: <select id="mySelect" datatextfield="ProductType" runat="server" />
<input type=submit onServerClick="getProducts" value="Get Gear" runat="server" />
<p>
<asp:datalist id="myDataList" runat="server"
width=90% repeatcolumns=2
itemstyle-width=50%>
<itemtemplate>
<table cellpadding=10>
<tr valign="top">
<td width=120><img align="top" src='<%# Eval ( "ProductImageURL" ) %>' ></td>
<td>
<b><%# Eval ( "ProductName" ) %></b>
<p><%# Eval ( "ProductDescription" ) %>
<p class="orderID">Code: <%# Eval ( "ProductCode" ) %>
<br>
<%# Eval ( "UnitPrice", "{0:c2}" ) %></p>
</td></tr>
</table>
</itemtemplate>
</asp:datalist>
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>