使用asp.net中的超链接更新sql列 [英] update sql column using a hyperlink in asp.net

查看:117
本文介绍了使用asp.net中的超链接更新sql列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sql数据源内部连接2个表(产品表[产品数量列] 客户产品表[ProductID,CustomerID,TotalProducts ,UpdatedProducts])

I have an sql datasource inner joined with 2 tables (product table[product quantity column] and customer product table[ProductID,CustomerID,TotalProducts, UpdatedProducts])

目前我使用sql按钮后面有这个代码:

Currently i have this code behind using a sql button:

using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
        {
            scn.Open();
            SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID", scn);
            cmd.ExecuteNonQuery();
        }

虽然它工作正常,点击按钮后,它会更新所有字段更新产品列但我想要做的是使用相同的功能,但使用超链接。此外,它只会更新特定字段。这是一张更清晰的图片:

though it works fine and upon button click, it updates all the fields in the updated products column but what i want to do is to do the same functionality but with a hyperlink. Also, it will only update a specific field. Here is an image to make it clearer:

更新:
到目前为止我得到了这个。
html:

UPDATE: so far i got this. html:

   <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="UpdateProduct" CommandArgument='<%#Eval("CustomerID")+","+ Eval("ProductID") %>' />
            </ItemTemplate>
        </asp:TemplateField>

代码背后:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Approve")
        {
            using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
            {
                scn.Open();
                SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID WHERE WHERE ProductID=@ProductID", scn);
                cmd.Parameters.AddWithValue("@ProductID", ID);
                cmd.ExecuteNonQuery();
            }
        }
    }

这里发生的事情

推荐答案

这是一个完整的例子。希望它可以帮到你:

Here is a complete example.Hope it helps you:

.ASPX:

 <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="CustomerID" />
            <asp:BoundField DataField="ProductID" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="UpdateProduct" CommandArgument='<%#Eval("CustomerID")+","+ Eval("ProductID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

代码落后:

  public partial class GridViewRowCommandExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                var p1 = new Product { CustomerID = 1, ProductID = 11 };
                var p2 = new Product { CustomerID = 2, ProductID = 22 };

                GridView1.DataSource = new List<Product> { p1, p2 };
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateProduct")
            {
                string[] parameters = e.CommandArgument.ToString().Split(',');
                int customerID = Int32.Parse(parameters[0]);
                int productID = Int32.Parse(parameters[1]);
                //Now that you know which row to update run the SQL update statement 
            }
        }
    }

    public class Product
    {
        public int CustomerID { get; set; }
        public int ProductID { get; set; }
    }

这篇关于使用asp.net中的超链接更新sql列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆