----------------------前台------------------------
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand">
<EditItemTemplate>
<table style="width: 100%; height: 180px;">
<tr>
<td class="style4">
商品名:</td>
<td class="style2">
<asp:TextBox ID="txtProductName" runat="server"
Text='<%# Eval("ProductName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
规格:</td>
<td class="style2">
<asp:TextBox ID="txtProductStandard" runat="server"
Text='<%# Eval("ProductStandard") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
包装率:</td>
<td class="style2">
<asp:TextBox ID="txtPackagingRatio" runat="server"
Text='<%# Eval("PackagingRatio") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
商品条码:</td>
<td class="style2">
<asp:TextBox ID="txtArticleNum" runat="server" Text='<%# Eval("ArticleNum") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
价格:</td>
<td class="style2">
<asp:TextBox ID="txtPrice" runat="server" Text='<%# Eval("Price") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
<asp:Button ID="btnUpdate" runat="server" CommandArgument='<%# Eval("PId") %>'
CommandName="update" Height="21px" Text="更新" />
</td>
<td class="style2">
<asp:Button ID="btnCancel" runat="server" CommandName="cancel" Text="取消" />
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
产品名:<asp:Label ID="lblProductName" runat="server"
Text='<%# Eval("ProductName") %>'></asp:Label>
<br />
规格:<asp:Label ID="lblProductStandard" runat="server"
Text='<%# Eval("ProductStandard") %>'></asp:Label>
<br />
包装率:<asp:Label ID="lblPackagingRatio" runat="server"
Text='<%# Eval("PackagingRatio") %>'></asp:Label>
<br />
商品条码:<asp:Label ID="lblArticleNum" runat="server"
Text='<%# Eval("ArticleNum") %>'></asp:Label>
<br />
超市价格:<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
<br />
<asp:Button ID="btnEdit" runat="server" Text="编辑" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="删除"
CommandArgument='<%# Eval("PId") %>' CommandName="delete" />
<br />
<br />
<asp:Button ID="btnGouWuChe" runat="server" CommandArgument='<%# Eval("PId") %>'
CommandName="Buy" Text="放入购物车" />
<br />
</ItemTemplate>
</asp:DataList>
<br />
</div>
</form>
</body>
----------------后台----------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindProduct();
}
}
private void bindProduct()
{
string sql = "select * from Product";
DataTable dt = SQLHelper.ExecuteDataTable(sql);
this.DataList1.DataSource = dt;
this.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 into ShoppingCart(ProductName,ProductStandard,PackagingRatio,ArticleNum,Price) values(@ProductName,@ProductStandard,@PackagingRatio,@ArticleNum,@Price)";
SqlParameter[] pms = new SqlParameter[] {
new SqlParameter("@ProductName",ProName),
new SqlParameter("@ProductStandard",ProStandarde),
new SqlParameter("@PackagingRatio",ProPackaging),
new SqlParameter("@ArticleNum",ProArtialeNum),
new SqlParameter("@Price",ProPrice)
};
SQLHelper.ExecuteNonQuery(sql, pms);
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
this.bindProduct();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string ProName=(e.Item.FindControl("txtProductName") as TextBox).Text;
string ProStandarde=(e.Item.FindControl("txtProductStandard") as TextBox).Text;
string ProPackaging=(e.Item.FindControl("txtPackagingRatio") as TextBox).Text;
string ProArtialeNum=(e.Item.FindControl("txtArticleNum") as TextBox).Text;
string ProPrice = (e.Item.FindControl("txtPrice") as TextBox).Text;
string sql = "update Product set ProductName=@ProductName,ProductStandard=@ProductStandard,PackagingRatio=@PackagingRatio,ArticleNum=@ArticleNum,Price=@Price where PId=@pid";
SqlParameter[] pms = new SqlParameter[]{
new SqlParameter("@ProductName",ProName),
new SqlParameter("@ProductStandard",ProStandarde),
new SqlParameter("@PackagingRatio",ProPackaging),
new SqlParameter("@ArticleNum",ProArtialeNum),
new SqlParameter("@Price",ProPrice),
new SqlParameter("@pid",e.CommandArgument)
};
SQLHelper.ExecuteNonQuery(sql, pms);
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
this.bindProduct();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string sql = "delete from Product where PId=@pid";
SqlParameter pms = new SqlParameter("@pid", e.CommandArgument);
SQLHelper.ExecuteNonQuery(sql, pms);
this.bindProduct();
}
------------------------Web.config:-----------------------------------
<connectionStrings>
<add name="studentConnectionString" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=111111"
providerName="System.Data.SqlClient" />
</connectionStrings>
----------------------SQLHelper类:-------------------------------------
public static String connStr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql,connStr);
if (pms != null)
{
adapter.SelectCommand.Parameters.AddRange(pms);
}
adapter.Fill(dt);
return dt;
}
------------Product表---------------
id,商品名称,规格,包装率,商品条码,超市价格
PId,ProductName,ProductStandard,PackagingRatio,
ArticleNum,Price
---------------ShoppingCart表 ---------------------
id,商品名称,规格,包装率,商品条码,超市价格
PId,ProductName,ProductStandard,PackagingRatio,
ArticleNum,Price
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand">
<EditItemTemplate>
<table style="width: 100%; height: 180px;">
<tr>
<td class="style4">
商品名:</td>
<td class="style2">
<asp:TextBox ID="txtProductName" runat="server"
Text='<%# Eval("ProductName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
规格:</td>
<td class="style2">
<asp:TextBox ID="txtProductStandard" runat="server"
Text='<%# Eval("ProductStandard") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
包装率:</td>
<td class="style2">
<asp:TextBox ID="txtPackagingRatio" runat="server"
Text='<%# Eval("PackagingRatio") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
商品条码:</td>
<td class="style2">
<asp:TextBox ID="txtArticleNum" runat="server" Text='<%# Eval("ArticleNum") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
价格:</td>
<td class="style2">
<asp:TextBox ID="txtPrice" runat="server" Text='<%# Eval("Price") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
<asp:Button ID="btnUpdate" runat="server" CommandArgument='<%# Eval("PId") %>'
CommandName="update" Height="21px" Text="更新" />
</td>
<td class="style2">
<asp:Button ID="btnCancel" runat="server" CommandName="cancel" Text="取消" />
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
产品名:<asp:Label ID="lblProductName" runat="server"
Text='<%# Eval("ProductName") %>'></asp:Label>
<br />
规格:<asp:Label ID="lblProductStandard" runat="server"
Text='<%# Eval("ProductStandard") %>'></asp:Label>
<br />
包装率:<asp:Label ID="lblPackagingRatio" runat="server"
Text='<%# Eval("PackagingRatio") %>'></asp:Label>
<br />
商品条码:<asp:Label ID="lblArticleNum" runat="server"
Text='<%# Eval("ArticleNum") %>'></asp:Label>
<br />
超市价格:<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
<br />
<asp:Button ID="btnEdit" runat="server" Text="编辑" CommandName="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="删除"
CommandArgument='<%# Eval("PId") %>' CommandName="delete" />
<br />
<br />
<asp:Button ID="btnGouWuChe" runat="server" CommandArgument='<%# Eval("PId") %>'
CommandName="Buy" Text="放入购物车" />
<br />
</ItemTemplate>
</asp:DataList>
<br />
</div>
</form>
</body>
----------------后台----------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindProduct();
}
}
private void bindProduct()
{
string sql = "select * from Product";
DataTable dt = SQLHelper.ExecuteDataTable(sql);
this.DataList1.DataSource = dt;
this.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 into ShoppingCart(ProductName,ProductStandard,PackagingRatio,ArticleNum,Price) values(@ProductName,@ProductStandard,@PackagingRatio,@ArticleNum,@Price)";
SqlParameter[] pms = new SqlParameter[] {
new SqlParameter("@ProductName",ProName),
new SqlParameter("@ProductStandard",ProStandarde),
new SqlParameter("@PackagingRatio",ProPackaging),
new SqlParameter("@ArticleNum",ProArtialeNum),
new SqlParameter("@Price",ProPrice)
};
SQLHelper.ExecuteNonQuery(sql, pms);
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
this.bindProduct();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string ProName=(e.Item.FindControl("txtProductName") as TextBox).Text;
string ProStandarde=(e.Item.FindControl("txtProductStandard") as TextBox).Text;
string ProPackaging=(e.Item.FindControl("txtPackagingRatio") as TextBox).Text;
string ProArtialeNum=(e.Item.FindControl("txtArticleNum") as TextBox).Text;
string ProPrice = (e.Item.FindControl("txtPrice") as TextBox).Text;
string sql = "update Product set ProductName=@ProductName,ProductStandard=@ProductStandard,PackagingRatio=@PackagingRatio,ArticleNum=@ArticleNum,Price=@Price where PId=@pid";
SqlParameter[] pms = new SqlParameter[]{
new SqlParameter("@ProductName",ProName),
new SqlParameter("@ProductStandard",ProStandarde),
new SqlParameter("@PackagingRatio",ProPackaging),
new SqlParameter("@ArticleNum",ProArtialeNum),
new SqlParameter("@Price",ProPrice),
new SqlParameter("@pid",e.CommandArgument)
};
SQLHelper.ExecuteNonQuery(sql, pms);
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
this.bindProduct();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string sql = "delete from Product where PId=@pid";
SqlParameter pms = new SqlParameter("@pid", e.CommandArgument);
SQLHelper.ExecuteNonQuery(sql, pms);
this.bindProduct();
}
------------------------Web.config:-----------------------------------
<connectionStrings>
<add name="studentConnectionString" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=111111"
providerName="System.Data.SqlClient" />
</connectionStrings>
----------------------SQLHelper类:-------------------------------------
public static String connStr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql,connStr);
if (pms != null)
{
adapter.SelectCommand.Parameters.AddRange(pms);
}
adapter.Fill(dt);
return dt;
}
------------Product表---------------
id,商品名称,规格,包装率,商品条码,超市价格
PId,ProductName,ProductStandard,PackagingRatio,
ArticleNum,Price
---------------ShoppingCart表 ---------------------
id,商品名称,规格,包装率,商品条码,超市价格
PId,ProductName,ProductStandard,PackagingRatio,
ArticleNum,Price