I have a RadGrid setup using automatic insert/update with an ObjectContainerDataSource. In my page's code-behind, I'm handling the data source's OnInserted and OnUpdated events and calling methods in my presenter (MVP pattern).
The question is this - suppose the presenter detects some business logic violation while it's running OnInserted. How exactly should this be handled so that the grid can display an error message? I'd like the grid to remain in edit mode and for the data the user entered to remain on the screen. I'm using popup edit mode, but I don't think it matters much. I just want to display an appropriate error message and have the user correct their mistake and resubmit.
The core of my confusion is that I don't see how to get from (1) a presenter throwing an exception via the ObjectContainerDataSource.OnInserted() event handler to (2) a way to handle this exception in a meaningful way in the RadGrid.ItemInserted event handler. Maybe some code will clarify.
protected void Page_Init(object sender, EventArgs e)
{
Grid.ItemInserted += new GridInsertedEventHandler(Grid_ItemInserted);
UsersDataSource.Inserted += new EventHandler<ObjectContainerDataSourceStatusEventArgs>(UsersDataSource_Inserted);
}
void UsersDataSource_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
try
{
// This might throw a business exception.
UserPresenter.OnUserInserted((UserDTO)e.Instance);
}
catch (Exception ex)
{
// If I suppress, the grid will never know about the exception. If I rethrow, Grid_ItemInserted will never get called.
// I really want to "bubble" the exception up to Grid_ItemInserted() so it can deal with it in a meaningful way.
}
}
void Grid_ItemInserted(object source, GridInsertedEventArgs e)
{
// Handle errors.
if (e.Exception != null)
{
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
DisplayMessage("User cannot be created. Reason: " + e.Exception.Message);
}
else
{
DisplayMessage("User created.");
}
}
The question is this - suppose the presenter detects some business logic violation while it's running OnInserted. How exactly should this be handled so that the grid can display an error message? I'd like the grid to remain in edit mode and for the data the user entered to remain on the screen. I'm using popup edit mode, but I don't think it matters much. I just want to display an appropriate error message and have the user correct their mistake and resubmit.
The core of my confusion is that I don't see how to get from (1) a presenter throwing an exception via the ObjectContainerDataSource.OnInserted() event handler to (2) a way to handle this exception in a meaningful way in the RadGrid.ItemInserted event handler. Maybe some code will clarify.
protected void Page_Init(object sender, EventArgs e)
{
Grid.ItemInserted += new GridInsertedEventHandler(Grid_ItemInserted);
UsersDataSource.Inserted += new EventHandler<ObjectContainerDataSourceStatusEventArgs>(UsersDataSource_Inserted);
}
void UsersDataSource_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
try
{
// This might throw a business exception.
UserPresenter.OnUserInserted((UserDTO)e.Instance);
}
catch (Exception ex)
{
// If I suppress, the grid will never know about the exception. If I rethrow, Grid_ItemInserted will never get called.
// I really want to "bubble" the exception up to Grid_ItemInserted() so it can deal with it in a meaningful way.
}
}
void Grid_ItemInserted(object source, GridInsertedEventArgs e)
{
// Handle errors.
if (e.Exception != null)
{
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
DisplayMessage("User cannot be created. Reason: " + e.Exception.Message);
}
else
{
DisplayMessage("User created.");
}
}