使用DataList实现 加入购物车,编辑,删除,更新,取消功能。要求连一个产品表。

----------------------前台------------------------
<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" />
                &nbsp;<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
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值