error handle gridview in updatepanel asp.net

92 views Asked by At

I put a gridview in an updatepanel. When user del a row that will cause a FK error, there will be no any message and error page showup because asyncpostback updatepanel.

I put an ScriptManager.RegisterStartupScript to show some message in the GridView2_RowDeleted event when e.AffectedRows=0.But it also not work(nothing happen). I think maybe SQL error happen before GridView2_RowDeleted event.

So, May someone give me some idear to this situation? What I need is to show message like alert or lable.text when a SQL error happen(gridview in updatepanel )

By now here is the code of GridView2_RowDeleted

    Private Sub GridView2_RowDeleted(sender As Object, e As GridViewDeletedEventArgs) Handles GridView2.RowDeleted

    If e.AffectedRows = 0 Then
        Label14.Text = "Can't Del because still material in this storage!"
        ScriptManager.RegisterStartupScript(Me.UpdatePanel2, Me.UpdatePanel2.GetType(), "mykey", "alert('Error:" & "Can't Del because still material in this storage!" & "');", True)
        UpdatePanel2.Update()            
    End If
End Sub
1

There are 1 answers

0
夜市小霸王 On

Thanks everyone to reply me. The alert dont showup because of two problems first:e.message.tostring maybe exist some word that affect alert message string.

second:we need "e.ExceptionHandled = True" to hanlde error ourself

I find the answer below.

Private Sub GridView1_RowDeleted(sender As Object, e As GridViewDeletedEventArgs) Handles GridView1.RowDeleted
    If (e.Exception Is Nothing) Then
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alert", "alert('del 1 rec');", True)
        GridView1.SelectedIndex = -1
    Else
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alert", "alert('Error:del 0 rec');", True)
       e.ExceptionHandled = True
    End If

End Sub