前台代码:
<div>
<asp:DataList ID="DataList1"runat="server" oneditcommand="DataList1_EditCommand"
onitemcommand="DataList1_ItemCommand"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand"
onupdatecommand="DataList1_UpdateCommand">
<EditItemTemplate>
<tablestyle="width:100%;">
<tr>
<td class="style2">
商品名:</td>
<td>
<asp:TextBox ID="TextBox1"runat="server" Text='<%# Eval("ProdctName")%>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
规格:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"Text='<%# Eval("ProdctStandard") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
包装率:</td>
<td>
<asp:TextBox ID="TextBox3"runat="server" Text='<%# Eval("PackagingRatio")%>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
商品条码:</td>
<td>
<asp:TextBox ID="TextBox4"runat="server" Text='<%# Eval("ArticleNum")%>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
价格:</td>
<td>
<asp:TextBox ID="TextBox5"runat="server" Text='<%# Eval("Price")%>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Button ID="btnUpdate"runat="server" Text="更新"
CommandArgument='<%#Eval("PId") %>' CommandName="update" />
</td>
<td>
<asp:Button ID="btnCancel"runat="server" Text="取消" CommandName="Cancel"/>
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
产品名:<asp:LabelID="Label1" runat="server" Text='<%#Eval("ProdctName") %>'></asp:Label>
<br />
规格:<asp:LabelID="Label2" runat="server" Text='<%# Eval("ProdctStandard")%>'></asp:Label>
<br />
包装率:<asp:LabelID="Label3" runat="server" Text='<%#Eval("PackagingRatio") %>'></asp:Label>
<br />
商品条码:<asp:LabelID="Label4" runat="server" Text='<%#Eval("ArticleNum") %>'></asp:Label>
<br />
超市价格:<asp:LabelID="Label5" runat="server" Text='<%#Eval("Price") %>'></asp:Label>
<br />
<asp:ButtonID="btnedit" runat="server" CommandName="edit"Text="编辑" />
<asp:ButtonID="btndelete" runat="server"CommandName="delete"
Text="删除" CommandArgument='<%# Eval("PId") %>' />
<br />
<asp:ButtonID="Button3" runat="server" CommandArgument='<%#Eval("PId") %>'
CommandName="Buy" Height="22px" Text="放入购物车"Width="120px" />
</ItemTemplate>
</asp:DataList>
<br />
</div>
后台代码:
private void DataListBind()
{
string sql = "select * fromProduct";
DataTable dt =SqlHelper.ExecuteDataTable(sql);
this.DataList1.DataSource = dt;
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source,DataListCommandEventArgs e)
{
if (e.CommandName == "Buy")
{
string ProName =(e.Item.FindControl("lblProductName") as Label).Text;
string ProStandarde =(e.Item.FindControl("lblProductStandard") as Label).Text;
string ProPackaging =(e.Item.FindControl("lblPackagingRatio") as Label).Text;
string ProArtialeNum =(e.Item.FindControl("lblArticleNum") as Label).Text;
string ProPrice =(e.Item.FindControl("lblPrice") as Label).Text;
string sql ="insert intoShoppingCart(ProductName,ProductStandard,PackagingRatio,ArticleNum,Price)values(@ProductName,@ProductStandard,@PackagingRatio,@ArticleNum,@Price)";
SqlParameter[] pms =new SqlParameter[] {
newSqlParameter("@ProductName",ProName),
newSqlParameter("@ProductStandard",ProStandarde),
new SqlParameter("@PackagingRatio",ProPackaging),
newSqlParameter("@ArticleNum",ProArtialeNum),
newSqlParameter("@Price",ProPrice)
};
SQLHelper.ExecuteNonQuery(sql, pms);
}
}
protected void DataList1_EditCommand(object source,DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex =e.Item.ItemIndex;
DataListBind();
}
protected void DataList1_UpdateCommand(objectsource, DataListCommandEventArgs e)
{
stringname=(e.Item.FindControl("TextBox1") as TextBox).Text.Trim();
stringStand=(e.Item.FindControl("TextBox2") as TextBox).Text.Trim();
stringratio=(e.Item.FindControl("TextBox3") as TextBox).Text.Trim();
stringnum=(e.Item.FindControl("TextBox4") as TextBox).Text.Trim();
stringprice=(e.Item.FindControl("TextBox5") as TextBox).Text.Trim();
string sql = "update Product setProdctName=@name,ProdctStandard=@standard,PackagingRatio=@ratio,ArticleNum=@num,Price=@pricewhere PId=@id";
SqlParameter[] prm = newSqlParameter[]{
newSqlParameter("name",name), newSqlParameter("standard",Stand), newSqlParameter("ratio",ratio),
newSqlParameter("num",num),new SqlParameter("price",price),newSqlParameter("id",e.CommandArgument)
};
SqlHelper.ExecuteNonQuery(sql,prm);
}
protected void DataList1_CancelCommand(objectsource, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
DataListBind();
}
protected void DataList1_DeleteCommand(objectsource, DataListCommandEventArgs e)
{
string sql = "delete fromProduct where PId=@id";
SqlHelper.ExecuteNonQuery(sql,newSqlParameter("id",e.CommandArgument));
DataListBind();
}