2012-05-16 31 views
0

我需要將確認刪除操作添加到網格。問題在於「刪除」鏈接的呈現方式。 我的網格是在vb.net後面的代碼中構建的。 我有這個ASP.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 

我需要添加一個JavaScript確認對話框時,該子被調用。我可以使用Windows窗體消息框來實現,但這在服務器上不起作用。

pleae幫助 喬

+0

您只需編寫一個JavaScript函數來處理按鈕的onclick事件,並根據提示結果阻止/允許回發。 – MarioDS

回答

5

不能顯示ASP.NET一個消息,因爲這將在服務器上顯示。所以你需要一個點擊刪除按鈕的javascript confirm。因此,您不需要先回發到服務器。您可以在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 
0

作爲替代方案,在狀態變化(保存,刪除,更新等)的網頁通知用戶的常用方法在你的HTML中有一個更新元素。基本上,一個標籤(或任何),你會設置更新的用戶。

「您的更改已成功保存。」 「」嘗試保存更新時遇到錯誤。「例如,。在風格上,您可以將紅色設置爲錯誤或您的編程心臟所需的任何顏色。

我喜歡這種方法,因爲彈出窗口總是讓我感覺更像是WinForm。要麼工作,所以我只是想我會建議另一種方法。