<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head runat="server">
<title>Handling Postbacks from a DataList Template</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="~/shared/fetchData_sql.cs" />
<script language="C#" runat="server">
void Page_Load ( Object source, EventArgs e ) {
if ( ! IsPostBack ) {
// make table for subjects
DataTable tblSubjs = fetchData ( "SELECT DISTINCT Subject FROM Books ORDER BY Subject" ).Tables [ 0 ];
// add each row as a menuitem
foreach ( DataRow row in tblSubjs.Rows ) {
MenuItem subj = new MenuItem ( row [ "Subject" ].ToString ( ) );
menuSubjs.Items.Add ( subj );
if ( subj.Text == "ASP.NET" ) subj.Selected = true;
}
// initialize selection
getBooks ( null, null );
}
}
void getBooks ( Object sender, MenuEventArgs e ) {
mvBooks.SetActiveView ( listView );
string query = "SELECT * FROM Books WHERE Subject = '" + menuSubjs.SelectedItem.Text + "' AND Price IS NOT NULL";
booksList.DataSource = fetchReader ( query );
booksList.DataBind ( );
}
void getBook ( Object sender, EventArgs e ) {
string titleId = booksList.DataKeys [ booksList.SelectedItem.ItemIndex ].ToString ( );
string query = "SELECT * FROM Books WHERE TitleId = '" + titleId + "'";
bookSpecs.DataSource = fetchReader ( query );
bookSpecs.DataBind ( );
mvBooks.SetActiveView ( detailsView );
}
void resetView ( Object sender, EventArgs e ) {
getBooks ( null, null );
}
</script>
</head>
<body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<!-- #include file="bc_header.inc" -->
<center>
<form runat="server">
<table align="center" cellpadding=10>
<tr><td>
<asp:menu id="menuSubjs" runat="server" onmenuitemclick="getBooks" orientation="horizontal">
<staticmenustyle backcolor="whitesmoke" horizontalpadding=10 verticalpadding=6
borderstyle="outset" borderwidth=2 />
<staticmenuitemstyle horizontalpadding=10 font-bold />
<statichoverstyle backcolor="beige" forecolor="maroon" />
<staticselectedstyle backcolor="khaki" forecolor="maroon" />
</asp:menu>
</td></tr>
</table>
<asp:multiview id=mvBooks runat=server>
<asp:view id=listView runat=server>
<asp:datalist id="booksList" runat="server"
datakeyfield="titleid" width=90% repeatcolumns=2
itemstyle-width=50% onSelectedIndexChanged="getBook">
<itemtemplate>
<table cellpadding=10>
<tr>
<td valign="top">
<img align="top" width="40" border=1 runat="server"
src='<%# Eval ( "titleid", "~/shared/books/sm/{0}.jpg" ) %>'
style="box-shadow: 3px 3px 6px gray" />
</td>
<td valign="top">
<b>Title:</b> <asp:linkbutton runat="server"
text='<%# Eval ( "title" ) %>' CommandName="Select" style="color:darkred" />
<br>
<b>Price:</b> <%# Eval ( "price", "{0:c2}" ) %><br>
</td></tr>
</table>
</itemtemplate>
</asp:datalist>
</asp:view>
<asp:view id=detailsView runat=server>
<asp:formview id="bookSpecs" runat="server"
width=70% enableviewstate=false>
<itemtemplate>
<div class="header"><h2 style="color:maroon"><%# Eval ( "title" ) %></h2></div>
<table cellpadding=10>
<tr>
<td valign="top">
<a href='<%# Eval ( "url" ) %>' target=books>
<img align="top" runat="server"
src='<%# Eval ( "titleid", "~/shared/books/lrg/{0}.jpg" ) %>'
style="width: 300; box-shadow: 3px 3px 6px gray" /></a>
</td>
<td valign="top">
<table>
<tr bgcolor="beige">
<td colspan=2 style="padding:10; border:2px inset">
<p><%# Eval ( "shortdesc" ) %></p>
</td>
</tr>
<tr><td><p> </p></td></tr>
<tr style="font-size:11pt">
<td width=40%>
<p><b>Category:</b> <%# Eval ( "subject" ) %></p>
<p><b>Title ID:</b> <%# Eval ( "titleid" ) %></p>
<p><b>Author:</b> <%# Eval ( "author" ) %></p>
<p><b>Publisher:</b> <%# Eval ( "publisher" ) %></p>
<p><b>Price:</b> <%# Eval ( "price", "{0:c}" ) %></p>
</td>
</tr>
</table>
</td></tr>
</table>
</itemtemplate>
</asp:formview>
<p><asp:button text="Back To List" onclick="resetView" runat=server />
</asp:view>
</asp:multiview>
</form>
</center>
<!-- #include file="bc_footer.inc" -->
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>