Hello Andrew,
Thanks for your quick reply.
Yes, if you'll use Server.Transfer (server-side redirection) later,
client-script approach won't work. For server-side redirection case, I
think you may consider set a flag (a page variable ) to indicate that
whether the request should be redirected and do the transfer/redirect in a
later event(such as Page_PreRender...).
And as for the new problem you mentioned:
====================
The exception thrown in the ObjectDataSource Inserted event does not stop
the redirect in the FormView ItemInserted event. e.g.
protected void odsAccount_Inserted(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
throw e.Exception.InnerException;
}
}
=======================
this is due to the internal exception handling codelogic of the FormView
and the underlying DataSourceView it calls(for your scenario, it's the
ObjectDataSourceView). As you've also found, the FormView.ItemInserted
event can help capture the exceptions occured during the insert operation
and expose it thourgh the event argument:
#FormView.ItemInserted Event
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.
iteminserted.aspx
Of course, this is the particular implementation of FormView (and some
other template databound control), for some simple webcontrols, there won't
have such additional exception management code logic. Actually the
detailed code logic of the FormView's ItemInserting process is as below:
1) We use postback event to trigger the FormView's Insert or manually call
FormView.InsertItem programmtically
2) FormView call its internal "HandleInsert" method which call the
DataSourceView(obtain from datasource control)'s Insert method.
DatasourceView.Insert is an asynchornous method and FormView will pass an
callbackhandler.
3) and in DataSourceView.Insert method, there is a large try...catch....
block which will capture any exceptions and return it (without rethrow it
to the original control). The code logic is like:
4)The callbackhandler(in FormView) will construct a ItemInsertedArgument
and call the OnItemInserted method which trigger the ItemInseretd event.
#"callback" here is the callbackhandler passed in 2)
=======================
try
{
num1 = this.ExecuteInsert(values);
}
catch (Exception exception1)
{
flag1 = true;
if (!callback(num1, exception1))
{
throw;
}
return;
}
finally
{
if (!flag1)
{
callback(num1, null);
}
}
========================
Thus, you can see why you'll get the exception through the event argumenet
in the FormView.ItemInserted event. If you have interests, you can use the
reflector tool to inspect the diassembly code of the FormView and
DataSourceView for a clearer view.
If you have anything unclear above or anything else we can help, please
feel free to let me know.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.