Asp.net Gridview CommandField验证有效,但不触发Delete方法

Asp.net Gridview CommandField验证有效,但不触发Delete方法,asp.net,validation,gridview,rowdatabound,rowdeleting,Asp.net,Validation,Gridview,Rowdatabound,Rowdeleting,我有一个gridview,我正试图从代码背后验证它。在这种情况下,确认记录删除。我的删除方法可以正常工作,直到我添加了验证,当我实现它时,它不会触发删除 要明确的是,在添加RowDataBound验证之前,删除方法可以正常工作 <asp:CommandField ButtonType="Image" ShowEditButton="true" ShowDeleteButton="true" ShowCancelButto

我有一个gridview,我正试图从代码背后验证它。在这种情况下,确认记录删除。我的删除方法可以正常工作,直到我添加了验证,当我实现它时,它不会触发删除

要明确的是,在添加RowDataBound验证之前,删除方法可以正常工作

<asp:CommandField 
        ButtonType="Image"
        ShowEditButton="true" 
        ShowDeleteButton="true" 
        ShowCancelButton="true"
        EditImageUrl="~/Images/edit-icon.gif" 
        UpdateImageUrl="~/Images/save-icon.png"
        CancelImageUrl="~/Images/cancel-icon.png" 
        DeleteImageUrl="~/Images/delete-icon.png" 
        />

您可以使用以下代码(我希望您了解C语言,足以将其翻译成VB.NET):

void GridView1\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var deleteButton=e.Row.Cells[0]。Controls.OfType().FirstOrDefault(btn=>btn.CommandName==“Delete”);
如果(deleteButton!=null)
{
deleteButton.OnClientClick=
string.Format(“javascript:if(确认('您确定要删除此用户吗?此操作无法撤消!”){{{{0}',delete${1}');}}}返回false;”,GridView1.UniqueID,e.Row.RowIndex);
}
}
}

原因是ImageButton控件使用
onclick
属性触发回发,而您的代码完全删除了它的默认值,并且根本没有发生回发。

问题不太清楚。当您说“它不会触发删除”时,是否会发生错误?是否未触发
行删除
行数据绑定
事件?否,未发生错误。如果我省略了在RowDataBound中使用的验证,则会按预期进行删除。如果我保留这个方法,什么也不会发生。没有错误,但不会删除。
Protected Sub usersGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
For Each control As Control In e.Row.Cells(0).Controls
        Dim DeleteButton As ImageButton = TryCast(control, ImageButton)
        If DeleteButton IsNot Nothing AndAlso DeleteButton.CommandName = "Delete" Then
            DeleteButton.OnClientClick = "return(confirm('Are you sure you want to delete this user?\nThis cannot be undone!'))"
        End If
    Next
End Sub

Protected Sub usersGrid_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
    Dim userID As Integer = DirectCast(usersGrid.DataKeys(e.RowIndex).Value, Integer)

    usersGrid_Delete(userID)
    BindData()
End Sub

Protected Sub usersGrid_Delete(ByVal userID As Integer)
    Dim con As New SqlConnection(connectionString)
    Dim cmd As New SqlCommand("MAINT_DIST_DELETE_USER", con)
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.AddWithValue("@userID", userID)

    Try
        con.Open()
        cmd.ExecuteNonQuery()
    Catch Ex As Exception
        Throw Ex
    Finally
        con.Close()
    End Try
End Sub
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var deleteButton = e.Row.Cells[0].Controls.OfType<ImageButton>().FirstOrDefault(btn => btn.CommandName == "Delete");
        if (deleteButton != null)
        {
            deleteButton.OnClientClick =
                string.Format("javascript:if(confirm('Are you sure you want to delete this user? This cannot be undone!')){{ __doPostBack('{0}', 'Delete${1}');}} return false;", GridView1.UniqueID, e.Row.RowIndex);
        }
    }
}