2009-12-01 29 views
13

Dove si verificano eccezioni negli UpdatePanel che ho implementato nell'applicazione web ASP.NET che sto creando, causano un errore JavaScript nella pagina con un output di errore di alto livello disponibile nell'avviso. Questo è ok per lo sviluppo, ma non appena il sistema è in produzione, ovviamente non va bene per molteplici ragioni. Posso circondare attività fastidiose con Try Catch ecc per controllare l'errore Javascript, ma in alcuni casi, voglio intraprendere azioni sulla pagina principale, ecc. Per supportare l'esperienza dell'utente.UpdatePanel Exception Handling

Come faccio a gestire gli errori che si verificano in UpdatePanels con garbo per fornire un'implementazione senza errori e Javascript senza errori?

risposta

18

È possibile utilizzare una combinazione della manifestazione AsyncPostBackError sul ScriptManager (lato server) e l'evento EndRequest sul PageRequestManager (lato client) per gestire completamente gli errori lato server quando si utilizza l'UpdatePanel.

ecco un paio di risorse che dovrebbe aiutare:

Customizing Error Handling for ASP.NET UpdatePanel Controls

Error Handling Customization for ASP.NET UpdatePanel

Ecco un semplice esempio:

// Server-side 
protected void ScriptManager1_AsyncPostBackError(object sender, 
    AsyncPostBackErrorEventArgs e) { 
    ScriptManager1.AsyncPostBackErrorMessage = 
     "An error occurred during the request: " + 
     e.Exception.Message; 
} 


// Client-side 
<script type="text/javascript"> 
    function pageLoad() { 
    Sys.WebForms.PageRequestManager.getInstance(). 
     add_endRequest(onEndRequest); 
    } 

    function onEndRequest(sender, args) { 
    var lbl = document.getElementById("Label1"); 
    lbl.innerHTML = args.get_error().message; 
    args.set_errorHandled(true); 
    } 
</script> 
+0

Grazie per questo, molto apprezzato. – Chris

+0

Questo ha funzionato benissimo dopo aver corretto un altro problema correlato: https://stackoverflow.com/questions/1671881/scriptmanager1-asyncpostbackerrormessage-not-showing-error-message – madamission

0

È possibile sovrascrivere il metodo di errore del livello di pagina, rilevare l'eccezione e gestire come si ritiene opportuno.

protected override void OnError(EventArgs e) 
{ 
    //show error message here 
} 
9

ho scritto qui circa un modo più semplice per farlo this: http://www.wagnerdanda.me/2010/01/asp-net-ajax-updatepanel-error-exception-handling-the-simple-way/

Se si desidera solo per risolvere il browser javascript errore e visualizzare il messaggio di eccezione per l'utente, è solo bisogno di aggiungere questo alla tua Masterpage da qualche parte dopo la dichiarazione di modulo:

<!-- This script must be placed after the form declaration --> 
<script type="text/javascript"> 
    Sys.Application.add_load(AppLoad); 

    function AppLoad() { 
     Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest); 
    } 

    function EndRequest(sender, args) { 
     // Check to see if there's an error on this request. 
     if (args.get_error() != undefined) { 

      var msg = args.get_error().message.replace("Sys.WebForms.PageRequestManagerServerErrorException: ", ""); 

      // Show the custom error. 
      // Here you can be creative and do whatever you want 
      // with the exception (i.e. call a modalpopup and show 
      // a nicer error window). I will simply use 'alert' 
      alert(msg); 

      // Let the framework know that the error is handled, 
      // so it doesn't throw the JavaScript alert. 
      args.set_errorHandled(true); 
     } 
    } 
</script> 

Non è necessario prendere OnAsyncPostBackError anche se non si desidera personalizzare il messaggio. Go to my blog post if you want more information about this.

+0

Worked! Molto apprezzato! –