【问题标题】:ASP.NET confirm delete in a gridASP.NET 确认在网格中删除
【发布时间】:2012-05-24 03:43:03
【问题描述】:

我需要向网格添加确认删除操作。问题是“删除”链接的呈现方式。 我的网格是在 vb.net 后面的代码中构建的。 我有这个

colDelete.AllowDelete = True
colDelete.Width = "100"
AddHandler CType(gridSavedForLater, Grid).DeleteCommand, AddressOf dgDeleteSelectedIncident

子如下

Sub dgDeleteSelectedIncident(ByVal sender As Object, ByVal e As GridRecordEventArgs)

    Dim message As String = "Are you sure you want to delete this incident?"
    Dim caption As String = "Confirm Delete"
    Dim result = System.Windows.Forms.MessageBox.Show(message, caption, Windows.Forms.MessageBoxButtons.OKCancel, Windows.Forms.MessageBoxIcon.Warning)

    'If (result = Windows.Forms.DialogResult.Cancel) Then
    'Else
    ' MessageBox("Are you sure you want to delete this incident?")
    'Get the VehicleId of the row whose Delete button was clicked
    'Dim SelectedIncidentId As String = e.Record("IncidentId")

    ''Delete the record from the database
    'objIncident = New Incidents(SelectedIncidentId, Session("userFullName"))

    'objIncident.DeleteIncident()
    ''Rebind the DataGrid
    LoadSavedForLater()
    ''        End If

End Sub

当调用这个 sub 时,我需要添加一个 javascript 确认对话框。我可以使用 Windows 窗体消息框来做到这一点,但这在服务器上不起作用。

请帮忙 乔

【问题讨论】:

  • 您只需要编写一个javascript函数来处理按钮的onclick事件并根据提示结果阻止/允许回发。

标签: asp.net vb.net


【解决方案1】:

作为替代方法,在网页中通知用户状态更改(保存、删除、更新等)的常用方法是在 HTML 中添加更新元素。基本上,您将为用户设置一个标签(或其他标签)。

“您的更改已成功保存。”“尝试保存更新时遇到错误。” 例如。您可以将红色设置为错误或任何您的编程心需要的颜色,风格。

我有点喜欢这种方法,因为弹出窗口对我来说总是更像 WinForm 的东西。要么工作,所以我只是想我会建议另一种方法。

【讨论】:

    【解决方案2】:

    您不能在 ASP.NET 中显示 MessageBox,因为它会显示在服务器上。所以你需要一个 javascript confirm onclick 的删除按钮。因此,您不需要先回发到服务器。您可以在GridView的初始加载时附加脚本。

    一个好地方是RowDataBoundGridView

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Select Case e.Row.RowType
            Case DataControlRowType.DataRow
                ' if it's a custom button in a TemplateField '
                Dim BtnDelete = DirectCast(e.Row.FindControl("BtnDelete"), Button)
                BtnDelete.OnClientClick = "return confirm('Are you certain you want to delete?');"
                ' if it's an autogenerated delete-LinkButton: '
                Dim LnkBtnDelete As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton)
                LnkBtnDelete.OnClientClick = "return confirm('Are you certain you want to delete?');"            
        End Select
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2023-04-11
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      相关资源
      最近更新 更多