<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>Navigating thru the Rows of a DataTable</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style type="text/css">
.desc {
font:bold 11pt arial,verdana,sans-serif; text-indent:25;
background:khaki; border:1px inset; padding:10; margin-bottom:10}
.features {
font-size:9pt; background:#efefef;
border:1px inset; padding-right:5; margin-bottom:10}
.specs {
font-size:9pt; background:beige;
border:1px inset; padding-right:5}
sup {font-size:7pt}
</style>
<script language="C#" runat="server" src="fetchData_oledb.cs" />
<script runat="server">
int rowIndex, rowCount;
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
string query = "SELECT DISTINCT Type FROM Plans WHERE NOT Type=''";
lstTypes.DataSource = fetchData ( query, "Plans" );
lstTypes.DataBind ( );
getPlans ( null, null );
}
}
void getPlans ( Object src, EventArgs e ) {
rowIndex = 1;
string query = "SELECT * FROM Plans WHERE Type='" + lstTypes.SelectedItem + "'";
DataTable tblSource = fetchData ( query, "plans" ).Tables [ 0 ];
rowCount = tblSource.Rows.Count;
// refresh session data
Session [ "tblSource" ] = tblSource;
Session [ "rowCount" ] = rowCount;
Session [ "rowIndex" ] = rowIndex;
DataView myView = getView ( rowIndex );
// bind datalist
myList.DataSource = myView;
myList.DataBind ( );
setButtons ( );
lblTracker.Text = "Record " + rowIndex + " of " + rowCount +
" " + lstTypes.SelectedItem + " Home Plans";
}
DataView getView ( int rowIndex ) {
DataTable tblSource = ( DataTable ) Session [ "tblSource" ];
tblSource.DefaultView.RowFilter = "PlanID='" +
tblSource.Rows [ rowIndex-1 ] [ "PlanId" ] + "'";
return tblSource.DefaultView;
}
void navigateList ( Object src, EventArgs e ) {
string direction = ( ( Button ) src ).CommandArgument;
rowIndex = ( int ) Session [ "rowIndex" ];
rowCount = ( int ) Session [ "rowCount" ];
switch ( direction ) {
case ( "first" ) :
rowIndex = 1;
break;
case ( "prev" ) :
if ( rowIndex > 1 ) rowIndex --;
break;
case ( "next" ) :
if ( rowIndex < rowCount ) rowIndex ++;
break;
case ( "last" ) :
rowIndex = rowCount;
break;
}
DataView myView = getView ( rowIndex );
// bind datalist
myList.DataSource = myView;
myList.DataBind ( );
setButtons ( );
lblTracker.Text = "Record " + rowIndex + " of " + rowCount +
" " + lstTypes.SelectedItem + " Home Plans";
// save current row number
Session [ "rowIndex" ] = rowIndex;
}
void setButtons ( ) {
// hide appropriate buttons to prevent needless postbacks
btnFirst.Visible = ( rowIndex > 1 );
btnPrev.Visible = ( rowIndex > 1 );
btnNext.Visible = ( rowIndex < rowCount );
btnLast.Visible = ( rowIndex < rowCount );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Navigating thru the Rows of a DataTable</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<form runat="server">
<center>
<table width=92% cellpadding=5 cellspacing=0 style="margin-bottom:8pt">
<tr style="background:steelblue" valign="middle">
<td width=25%><asp:dropdownlist id="lstTypes" runat="server"
datatextfield="Type" autopostback
onSelectedIndexChanged="getPlans" /></td>
<td width=50% align="center">
<b><asp:label id="lblTracker" style="color:snow" runat="server" /></b>
</td>
<td width=25% align="right">
<asp:button id="btnFirst" runat="server"
text="First"
commandargument="first"
font-bold
backcolor="brown"
forecolor="snow"
onClick="navigateList" />
<asp:button id="btnPrev" runat="server"
text="Back"
commandargument="prev"
font-bold
backcolor="brown"
forecolor="snow"
onClick="navigateList" />
<asp:button id="btnNext" runat="server"
text="Next"
commandargument="next"
font-bold
backcolor="brown"
forecolor="snow"
onClick="navigateList" />
<asp:button id="btnLast" runat="server"
text="Last"
commandargument="last"
font-bold
backcolor="brown"
forecolor="snow"
onClick="navigateList" />
</td></tr>
</table>
<asp:datalist id="myList" width=92% runat="server"
enableviewstate=false>
<itemtemplate>
<table width=100%>
<tr valign="top">
<td>
<div class="header">
<h3 style="color:maroon">
<%# Eval ( "Model" ) %>
</h2>
</div></td>
<td align="right">
<div class="header">
<h2>Plan # <%# Eval ( "PlanID" ) %></h2>
</div></td></tr>
</table>
<table width=100% cellpadding=10 style="background:ddd; border:2px outset">
<tr valign="top">
<td align="center" width=290>
<img width=290 runat="server"
src='<%# Eval ( "PlanID", "~/shared/images/plans/{0}.jpg" ) %>'
alt='<%# Eval ( "Model" ) %>'
style="border:1px inset">
<p><!-- spacer --></p>
<table width=90% class="data" cellspacing=0
style="font-size:8pt; text-align:center; border:1px inset">
<tr><th><%# Eval ( "Design" ) %></th></tr>
<tr><td><%# Eval ( "Area" ) %> ft<sup>2</sup>,
<%# Eval ( "Bedrooms" ) %> Bedrooms,
<%# Eval ( "Baths" ) %> Baths
<p>National Average Cost to Build: <br>
<b style="color:maroon">
<%# Eval ( "Cost", "{0:c}" ) %>
</b>
</td></tr>
</table>
</td>
<td>
<div class="desc"><%# Eval ( "Description" ) %></div>
<div class="features"><%# Eval ( "Features" ) %></div>
<div class="specs"><%# Eval ( "Specs" ) %></div>
</td></tr>
</table>
</itemtemplate>
</asp:datalist>
</center>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>