New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Error Handling Automatic Operations
RadGrid can fire three events after an automatic action occurred:
-
ItemUpdated
-
ItemInserted
-
ItemDeleted
C#
protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
{
if (e.Exception != null)
{
e.KeepInEditMode = true;
e.ExceptionHandled = true;
DisplayMessage("Product " + e.Item["ProductID"].Text + " cannot be updated. Reason: " + e.Exception.Message);
}
else
{
DisplayMessage("Product " + e.Item["ProductID"].Text + " updated");
}
}
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
DisplayMessage("Product cannot be inserted. Reason: " + e.Exception.Message);
}
else
{
DisplayMessage("Product inserted");
}
}
protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
DisplayMessage("Product " + e.Item["ProductID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
}
else
{
DisplayMessage("Product " + e.Item["ProductID"].Text + " deleted");
}
}
private void DisplayMessage(string text)
{
RadGrid1.Controls.Add(new LiteralControl(text));
}
The default behavior of Telerik RadGrid is tolet the DataSource control rise an exceptionwhen error occurs when inserting/updating/deleting. To prevent this exception you should handle the corresponding event and in case (e.Exception != null) or (Not e.Exception Is Nothing) you should set e.ExceptionHandled to true and display error message.
C#
protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
DisplayMessage("Product " + e.Item["ProductID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
}
....
}