HTML代码是:
<asp:DataGrid Runat="server" ID="ShopCartList" CssClass="Normal" AutoGenerateColumns="False" Width="100%"
BorderColor="NavajoWhite">
<Columns>
<asp:TemplateColumn HeaderText="商品种类" ItemStyle-Width="80" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"CategoryDescn") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品名称" ItemStyle-Width="140" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<a href='Product.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID") %>'>
<%# DataBinder.Eval(Container.DataItem,"ProductDescn") %>
</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品单价" ItemStyle-Width="80" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Price" Width="100%" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Price") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品数量" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="Quantity" Runat="server" Width="60" Text='<%# DataBinder.Eval(Container.DataItem,"Quantity") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="购买时间" ItemStyle-Width="100" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Pubdate","{0:d}") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除商品" ItemStyle-Width="100" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="DeleteProduct" ImageUrl="~/Images/delete.gif" Runat="server" CssClass="CommandButton" CommandName="delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ProductID") %>'>
</asp:ImageButton>
</asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
后台函数是
private void ProductList_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(Session["UserID"] != null)
{
Button buyProduct = (Button)e.Item.FindControl("BuyProduct");
if(buyProduct != null)
{
//从数据库中取到用户的购物车
int nShopCartID = 0;
ShopCartDAL shopcart = new ShopCartDAL();
SqlDataReader recs = shopcart.GetShopCartByUser(Int32.Parse(Session["UserID"].ToString()));
if(recs.Read())
{
nShopCartID = Int32.Parse(recs["ShopCartID"].ToString());
}
recs.Close();
if(nShopCartID == 0)
{
///创建一个新的购物车
nShopCartID = shopcart.AddShopCart(Int32.Parse(Session["UserID"].ToString()));
}
if(nShopCartID > 0)
{
//把商品添加到购物车中
ProductCartDAL shopingCart = new ProductCartDAL();
shopingCart.AddProductCart(nShopCartID,Int32.Parse(ProductList.DataKeys[e.Item.ItemIndex].ToString()),1);
Response.Write("<script>alert(\"添加商品到购物车成功!\")</script>");
}
}
}
else
{
Response.Write("<script>alert(\"你还没有登录,请先登陆系统!\")</script>");
}
}
请问如何使Button和ProductList_ItemCommand()函数关联 谢谢了~着急,那个大虾愿意的话请留联系方式,事成我必定会感谢你的,谢谢了