<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<title>Introduction to Templated Controls ( Gridview )</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" src="~/shared/fetchData_sql.cs" runat="server" />
<script language="C#" runat="server">
void Page_Load ( Object sender, EventArgs e ) {
if ( !IsPostBack ) {
string query = "SELECT DISTINCT Subject FROM Books WHERE Price IS NOT NULL";
lstSubjects.DataSource = fetchReader ( query );
lstSubjects.DataBind ( );
//initialize selection
lstSubjects.SelectedIndex = 1;
getBooks ( null, null );
}
}
void getBooks ( Object sender, EventArgs e ) {
string subject = lstSubjects.Text;
string query = "SELECT * FROM Books WHERE Subject = '" + subject + "' AND Price IS NOT NULL";
myGrid.DataSource = fetchReader ( query );
myGrid.DataBind ( );
}
</script>
<body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<!-- #include file="dl_header.inc" -->
<center>
<form runat="server">
<p>Select Category: <asp:dropdownlist id="lstSubjects" runat="server"
autopostback onSelectedIndexChanged="getBooks"
datatextfield="Subject" /></p>
<asp:gridview width="94%" id="myGrid" runat="server"
autogeneratecolumns=false
cellpadding=5 cellspacing=0
backcolor="ghostwhite"
font-size="10pt"
enableviewstate=false>
<headerstyle height=30 font-bold
backcolor="steelblue"
forecolor="beige" />
<columns>
<asp:imagefield headertext=""
dataimageurlfield="titleid"
dataimageurlformatstring="/shared/books/sm/{0}.jpg" />
<asp:hyperlinkfield headertext="Title"
datatextfield="title" target="_blank"
datanavigateurlfields="titleid"
datanavigateurlformatstring="https://www.amazon.com/dp/{0}?&tag=rvnunz0525-20" />
<asp:boundfield headertext="Author"
datafield="Author" />
<asp:boundfield headertext="Publisher"
datafield="Publisher" />
<asp:boundfield headertext="Price (from)"
datafield="Price"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
</form>
</center>
<!-- #include file="dl_footer.inc" -->
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>