<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>ButtonColumn ButtonType Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style type="text/css">
<!--
.add {
background:navy; color:lime}
.remove {
background:maroon; color:gold}
-->
</style>
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script language="C#" runat="server">
DataTable Cart;
DataView CartView;
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
string query = "SELECT * FROM Titles where Price > 0";
myGrid.DataSource = fetchData ( query );
myGrid.DataBind ( );
}
if ( Session [ "shoppingSession" ] == null ) {
Cart = new DataTable ( );
Cart.Columns.Add ( new DataColumn ( "Item", typeof ( string ) ) );
Cart.Columns.Add ( new DataColumn ( "Price", typeof ( string ) ) );
Session [ "shoppingSession" ] = Cart;
}
else Cart = ( DataTable ) Session [ "shoppingSession" ];
CartView = new DataView ( Cart );
shopCart.DataSource = CartView;
shopCart.DataBind ( );
}
void updateCart ( Object src, DataGridCommandEventArgs e ) {
DataRow dr = Cart.NewRow ( );
// e.Item is the row of the table where the command is raised.
// for bound columns, values are stored in the Text property of Cells [ colIndex ]
string item = e.Item.Cells [ 1 ].Text;
string price = e.Item.Cells [ 2 ].Text;
if ( ( ( Button ) e.CommandSource ).CommandName == "AddToCart" ) {
dr [ 0 ] = item; dr [ 1 ] = price;
Cart.Rows.Add ( dr );
}
else { // remove from Cart
CartView.RowFilter = "Item='" + item + "'";
if ( CartView.Count > 0 ) CartView.Delete ( 0 );
CartView.RowFilter = "";
}
shopCart.DataBind ( );
}
void setButtonStyle ( Object src, DataGridItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
TableCellCollection myCells = e.Item.Cells;
( ( Button ) myCells [ 3 ].Controls [ 0 ] ).CssClass = "add";
( ( Button ) myCells [ 4 ].Controls [ 0 ] ).CssClass = "remove";
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>ButtonColumn ButtonType Example</h2></div>
<hr size=1 width=92%>
<form runat="server">
<table cellpadding="5">
<tr valign="top">
<td><b>Product List</b>
<asp:datagrid id="myGrid" runat="server"
cellpadding=5 font-size="9pt"
gridlines="horizontal"
autogeneratecolumns=false
onItemCreated="setButtonStyle"
onItemCommand="updateCart">
<headerstyle backcolor="darkslategray"
forecolor="khaki" font-bold />
<itemstyle backcolor="ivory" verticalalign="top" />
<columns>
<asp:boundcolumn
headertext="Title"
datafield="title" />
<asp:boundcolumn
headertext="Item"
datafield="title_id" />
<asp:boundcolumn
headertext="Price"
datafield="price"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right" />
<asp:buttoncolumn
text=" + "
buttontype="pushbutton"
commandname="AddToCart" />
<asp:buttoncolumn
text=" - "
buttontype="pushbutton"
commandname="RemoveFromCart" />
</columns>
</asp:datagrid>
</td>
<td><b>Shopping Cart</b>
<asp:datagrid id="shopCart" runat="server"
cellpadding=5 font-size="9pt"
headerstyle-backcolor="darkslategray"
headerstyle-forecolor="khaki"
headerstyle-font-bold
itemstyle-backcolor="ivory"
itemstyle-verticalalign="top" />
</td></tr>
</table>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>