Asp.net中用核选框显示数据的方法及ButtonColumn的使用方法
2024-07-10 12:58:23
供稿:网友
<%@ import namespace="system.data" %>
<html>
<script language="c#" runat="server">
datatable cart;
dataview cartview;
icollection createdatasource() {
datatable dt = new datatable();
datarow dr;
dt.columns.add(new datacolumn("integervalue", typeof(int32)));
dt.columns.add(new datacolumn("stringvalue", typeof(string)));
dt.columns.add(new datacolumn("datetimevalue", typeof(datetime)));
dt.columns.add(new datacolumn("boolvalue", typeof(bool)));
dt.columns.add(new datacolumn("currencyvalue", typeof(double)));
for (int i = 0; i < 9; i++) {
dr = dt.newrow();
dr[0] = i;
dr[1] = "item " + i.tostring();
dr[2] = datetime.now;
dr[3] = (i % 2 != 0) ? true : false;
dr[4] = 1.23 * (i+1);
dt.rows.add(dr);
}
dataview dv = new dataview(dt);
return dv;
}
void page_load(object sender, eventargs e)
{
if (session["dg5_shoppingcart"] == null) {
cart = new datatable();
cart.columns.add(new datacolumn("item", typeof(string)));
cart.columns.add(new datacolumn("price", typeof(string)));
session["dg5_shoppingcart"] = cart;
}
else {
cart = (datatable)session["dg5_shoppingcart"];
}
cartview = new dataview(cart);
shoppingcart.datasource = cartview;
cartview.sort="item";
shoppingcart.databind();
mydatagrid.datasource = createdatasource();
mydatagrid.databind();
}
void grid_cartcommand(object sender, datagridcommandeventargs e) {
datarow dr = cart.newrow();
// e.item is the row of the table where the command fired
// for bound columns the value is stored in the text property of tablecell
tablecell itemcell = e.item.cells[1];
tablecell pricecell = e.item.cells[2];
string item = itemcell.text;
string price = pricecell.text;
if (((linkbutton)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 = "";
}
shoppingcart.databind();
}
</script>
<body>
<h3><font face="verdana">using a template column in datagrid</font></h3>
<form runat=server>
<table cellpadding="5">
<tr valign="top">
<td>
<b>product list</b>
<asp:datagrid id="mydatagrid" runat="server"
bordercolor="black"
borderwidth="1"
gridlines="both"
cellpadding="3"
cellspacing="0"
font-name="verdana"
font-size="8pt"
headerstyle-backcolor="#aaaadd"
autogeneratecolumns="false"
onitemcommand="grid_cartcommand"
>
<columns>
<asp:templatecolumn headertext="add/remove">
<itemtemplate>
<asp:linkbutton id=addbutton text="add" commandname="addtocart" forecolor="blue" runat="server" />
<asp:linkbutton id=removebutton text="remove" commandname="removefromcart" forecolor="blue" runat="server" />
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn headertext="item" datafield="stringvalue"/>
<asp:boundcolumn headertext="price" datafield="currencyvalue" dataformatstring="{0:c}" itemstyle-horizontalalign="right" />
<asp:templatecolumn headertext="assembly required?">
<itemtemplate>
<asp:checkbox id=chk1 checked='<%# databinder.eval(container.dataitem, "boolvalue") %>' enabled="false" runat="server" />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</td><td>
<b>shopping cart</b>
<asp:datagrid id="shoppingcart" runat="server"
bordercolor="black"
borderwidth="1"
cellpadding="3"
font-name="verdana"
font-size="8pt"
headerstyle-backcolor="#aaaadd"
/>
</td>
</tr>
</table>
</form>
</body>
</html>