Custom Error Text for Callback Exception
- 3 minutes to read
If an unhandled exception occurs while a DevExpress web control’s callback is processed on the server, ASPxHttpHandlerModule automatically catches the exception and passes an error message to the client.
The error message can contain details about the exception or a generic error message based on the customErrors mode setting defined in Web.config.
You can use one of the following approaches to modify the error message that is sent to the client:
Call the SetCallbackErrorMessage Method
Call the static SetCallbackErrorMessage(String) method in the Application_Error or CallbackError event handler in the Global.asax file. In a handler, you can use the GetLastError() method to access the source error.
//Generate your own exception type
protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
throw new CustomExceptions.MyException("Editing is disabled. Please log in as administrator.");
}
//Change error text in CallbackError event handler using the SetCallbackErrorMessage method
void Application_Start(object sender, EventArgs e){
DevExpress.Web.ASPxWebControl.CallbackError += Callback_Error;
}
protected void Callback_Error(object sender, EventArgs e) {
var exception = HttpContext.Current.Server.GetLastError();
//Check exception type and specify error text for your exception
if(exception is CustomExceptions.MyException) {
DevExpress.Web.ASPxWebControl.SetCallbackErrorMessage(exception.Message);
}
}
Handle the CustomErrorText Event
The CustomErrorText event allows you to define custom error text. The event is implemented for the following controls:
- ASPxCardView.CustomErrorText
- ASPxFileManager.CustomErrorText
- ASPxGridView.CustomErrorText
- ASPxScheduler.CustomErrorText
- ASPxTreeList.CustomErrorText
- ASPxVerticalGrid.CustomErrorText
In the event handler, use the e.ErrorText property to define error text and the e.Exception property to access the source error and its error message.
//Generate your own exception type
protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
throw new CustomExceptions.MyException("Editing is disabled. Please log in as administrator.");
}
//Change error text for your exception type
protected void ASPxGridView1_CustomErrorText(object sender, DevExpress.Web.ASPxGridViewCustomErrorTextEventArgs e) {
if(e.Exception is CustomExceptions.MyException) {
e.ErrorText = e.Exception.Message;
}
}