[英]update sql column using a hyperlink in asp.net
I have an sql datasource inner joined with 2 tables (product table[product quantity column]
and customer product table[ProductID,CustomerID,TotalProducts, UpdatedProducts])
我有一个内部有2个表
(product table[product quantity column]
和customer product table[ProductID,CustomerID,TotalProducts, UpdatedProducts])
的sql数据源。
Currently i have this code behind using a sql button: 目前,我在使用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();
}
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. 尽管它工作正常并且单击按钮时,它会更新“
updated products column
中的所有字段,但我要做的是执行相同的功能但带有超链接。 Also, it will only update a specific field. 同样,它将仅更新特定字段。 Here is an image to make it clearer:
这是一个更清晰的图像:
UPDATE: so far i got this. 更新:到目前为止,我已经知道了。 html:
的HTML:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="UpdateProduct" CommandArgument='<%#Eval("CustomerID")+","+ Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
code behind: 后面的代码:
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();
}
}
}
nothings happening here 这里什么都没发生
Here is a complete example.Hope it helps you: 这是一个完整的示例,希望它对您有帮助:
.ASPX: .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>
Code behind: 后面的代码:
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; }
}
First put a button field with "ButtonFieldName" in GridView control and then try GridView Control's RowCommand event. 首先在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.