【发布时间】:2012-01-03 02:18:28
【问题描述】:
我有一个网格视图,我正试图从后面的代码中进行验证。在这种情况下,确认记录删除。我的删除方法工作正常,直到我添加验证,当我实现它时,它不会触发删除。
需要明确的是,在我添加 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"
/>
以下是相关方法。
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
【问题讨论】:
-
不是很清楚问题是什么。当您说“它不会触发删除”时,是否会发生错误?
RowDeleting或RowDataBound事件不会触发吗? -
不,没有发生错误。如果我遗漏了我在 RowDataBound 中使用的验证,则会按预期进行删除。如果我把这个方法留在里面,什么都不会发生。没有错误,但不会发生删除。
标签: asp.net validation gridview rowdatabound rowdeleting