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();
        }
ASP.NET中,你可以使用GridView控件来实现数据库表的增删改查操作。首先,你需要确保你已经建立了数据库连接。你可以使用引用中提供的数据库连接字符串来连接到你的数据库。 接下来,你可以在ASP.NET页面中添加一个GridView控件,并通过数据绑定将其与你的数据库表相关联。使用GridView控件的数据源属性来指定你要查询的表,然后使用数据绑定方法将数据库的数据绑定到GridView控件上。 要进行增加操作,你可以在GridView控件中的底部或顶部添加一个模板列,其中包含文本框和按钮。当用户填写相关信息并点击按钮时,你可以在后台代码中捕获这些信息,并将其插入到数据库表中。 要进行删除操作,你可以在GridView控件的每一行中添加一个按钮,然后在后台代码中捕获点击事件,并获取要删除的行的唯一标识符。然后,你可以使用SQL语句将该行从数据库表中删除。 要进行更新操作,你可以在GridView控件中的每一行中添加一个编辑按钮,当用户点击编辑按钮时,你可以将该行的数据加载到文本框中。用户可以在文本框中修改数据,并点击更新按钮将更改保存到数据库表中。 最后,你可以使用GridView控件的自带分页功能来实现分页查询。 请注意,以上步骤仅为概述,具体实现方式可能会因为你的项目需求和数据库结构而有所不同。你可以根据需要进行调整和扩展。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [ASP.NET GridView做增删改查操作](https://download.csdn.net/download/s756702559/14502523)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ASP.NET数据库的增删改查操作](https://blog.csdn.net/sichuanpb/article/details/122377068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值