简体   繁体   English

使用存储过程而不是使用SqlDataSource更新和删除GridView ASP.NET C#中的行

[英]Update and Delete rows in GridView ASP.NET C# using stored procedure instead of using SqlDataSource

I have a GridView to manage my products. 我有一个GridView来管理我的产品。 I have used store procedure to set data source for it. 我已使用存储过程为其设置数据源。 I also use AutoGenerateDeleteButton property for deleting each row of my GridView and now I want to use a stored procedure to delete the rows of my GridView when I click on "Delete" LinkButton. 我还使用AutoGenerateDeleteButton属性删除GridView的每一行,现在我想使用存储过程在单击“删除” LinkBut​​ton时删除GridView的行。 I tried some tutorial in other forum about ASP.NET but nothing happens. 我在其他论坛上尝试了一些有关ASP.NET的教程,但没有任何反应。

Thanks 谢谢

You can use item command. 您可以使用项目命令。 On item command you can pass the id of the row you want to delete as a command argument call your delete stored procedure and then rebind the datasource on the gridview using your select stored procedure. 在item命令上,您可以将要删除的行的id作为命令参数传递给调用delete存储过程,然后使用select存储过程在gridview上重新绑定数据源。

<asp:LinkButton runat="server" OnCommand="Delete_Command" CommandName="Delete" CommandArgument='<%# Eval("DeleteRowID") %>'></asp:LinkButton>

VB.NET VB.NET

Protected Sub Delete_Command(sender As Object, e As CommandEventArgs)  
If e.CommandName.ToString = "Delete" Then 
Dim DeleteRowId As String = e.CommandArgument.ToString 
'call procedure here and pass argument delete row id 
'ExambleGridView.DataSource= Call procedure here to get the datasource 
ExambleGridView.DataBind()
End If
End Sub

C# C#

  protected void Delete_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.ToString == "Delete") {
            string DeleteRowId = e.CommandArgument.ToString();
            //Call Procedure here to delete row
            ExampleGridview.Datasource=//Call Procedure here to get the datasource again
            ExampleGridView.Databind()
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM