簡體   English   中英

使用asp.net中的超鏈接更新sql列

[英]update sql column using a hyperlink in asp.net

我有一個內部有2個表(product table[product quantity column]customer product table[ProductID,CustomerID,TotalProducts, UpdatedProducts])的sql數據源。

目前,我在使用sql按鈕后有以下代碼:

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();
        }

盡管它工作正常並且單擊按鈕時,它會更新“ updated products column中的所有字段,但我要做的是執行相同的功能但帶有超鏈接。 同樣,它將僅更新特定字段。 這是一個更清晰的圖像:

在此處輸入圖片說明

更新:到目前為止,我已經知道了。 的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();
            }
        }
    }

這里什么都沒發生

這是一個完整的示例,希望它對您有幫助:

.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; }
    }

首先在GridView控件中放置一個帶有“ ButtonFieldName”的按鈕字段,然后嘗試GridView控件的RowCommand事件。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ButtonFieldName")
    {
       // Your Logic goes here
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM