asp.net控件快捷增删改查

前台代码:

<div>
        <asp:DataList ID="DataList1" runat="server" oneditcommand="DataList1_EditCommand" 
              onitemcommand="DataList1_ItemCommand" 
              oncancelcommand="DataList1_CancelCommand" 
              ondeletecommand="DataList1_DeleteCommand" 
              onupdatecommand="DataList1_UpdateCommand">
            <EditItemTemplate>
                <table style="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:Label ID="Label1" runat="server" Text='<%# Eval("ProdctName") %>'></asp:Label>
                <br />
                规格:<asp:Label ID="Label2" runat="server" Text='<%# Eval("ProdctStandard") %>'></asp:Label>
                <br />
                包装率:<asp:Label ID="Label3" runat="server" Text='<%# Eval("PackagingRatio") %>'></asp:Label>
                <br />
                商品条码:<asp:Label ID="Label4" runat="server" Text='<%# Eval("ArticleNum") %>'></asp:Label>
                <br />
                超市价格:<asp:Label ID="Label5" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
                <br />
                &nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="btnedit" runat="server" CommandName="edit" Text="编辑" />
                &nbsp;&nbsp;
                <asp:Button ID="btndelete" runat="server" CommandName="delete" 
                    Text="删除" CommandArgument='<%# Eval("PId") %>' />
                <br />
                &nbsp;&nbsp;
                <asp:Button ID="Button3" runat="server" CommandArgument='<%# Eval("PId") %>' 
                    CommandName="Buy" Height="22px" Text="放入购物车" Width="120px" />
            </ItemTemplate>
        </asp:DataList>
        <br />
    </div>

后台代码:


        private void DataListBind()
        {
            string sql = "select * from Product";
            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 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;
            DataListBind();
        }

        protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            string name=(e.Item.FindControl("TextBox1") as TextBox).Text.Trim();
            string Stand=(e.Item.FindControl("TextBox2") as TextBox).Text.Trim();
            string ratio=(e.Item.FindControl("TextBox3") as TextBox).Text.Trim();
            string num=(e.Item.FindControl("TextBox4") as TextBox).Text.Trim();
            string price=(e.Item.FindControl("TextBox5") as TextBox).Text.Trim();
            string sql = "update Product set ProdctName=@name,ProdctStandard=@standard,PackagingRatio=@ratio,ArticleNum=@num,Price=@price where PId=@id";
            SqlParameter[] prm = new SqlParameter[]{
              new SqlParameter("name",name), new SqlParameter("standard",Stand), new SqlParameter("ratio",ratio),
              new SqlParameter("num",num),new SqlParameter("price",price),new SqlParameter("id",e.CommandArgument)

            };
            SqlHelper.ExecuteNonQuery(sql,prm);
        }

        protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.DataList1.EditItemIndex = -1;
            DataListBind();
        }

        protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            string sql = "delete from Product where PId=@id";
            SqlHelper.ExecuteNonQuery(sql,new SqlParameter("id",e.CommandArgument));
            DataListBind();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值