First, when you press the commandName = "Insert" button in FormView,. NET will first execute the OnInserted event of object data source. This event has a parameter of the ObjectDataSourceStatusEventArgs type: it has several attributes that we will use:
1. e. Exception -- indicates the Exception object that occurs when the call is added. If an Exception occurs on the called target object, view the InnerException object to obtain more information.
2. e. ExceptionHandled -- indicates whether to mark the exception as handled. If it is set to true, the exception will not be passed to the subsequent event.
3. e. ReturnValue -- indicates the value returned by the InsertMethod you set on the object data source. You can check whether the value is successful.
Then ,. NET will execute the FormView OnItemInserted event. If the ExceptionHandled of the previous object data source is not set to true, Exception will be passed in to this event. This event has a FormViewInsertedEventArgss type parameter: it has several attributes that we will use:
1. e. ExceptionHandled -- indicates whether to mark the exception as handled. If it is set to true, the exception will not be passed to the Page. Error Event. Otherwise, if Page. error Event, first execute Page. error event, if the Page. error does not execute Server. clearError () method, exception passed in Application_Error event. page not defined. when an Error event is handled, the Application_Error event is directly passed in.
2. e. keepinsertmode -- whether to keep the Insert mode. Normally, if an error occurs, an error message is displayed, which is kept in the Insert mode for users to modify.
Protected override void OnError (EventArgs e)
{
// If fvFixingResult_ItemInserted and objdsFixingResult_Inserted
// Didn't set ExceptionHandled = true, this event will be called.
Exception ex = Server. GetLastError ();
If (ex! = Null)
{
LblErrorMessage. Text = ex. Message;
}
Server. ClearError ();
}
Protected void fvFixingResult_ItemInserted (object sender, FormViewInsertedEventArgs e)
{
If (! E. ExceptionHandled & e. Exception! = Null)
{
E. ExceptionHandled = true;
E. KeepInInsertMode = true;
}
Else
{
CloseWindow ();
}
}
Protected void objdsFixingResult_Inserted (object sender, ObjectDataSourceStatusEventArgs e)
{
If (e. Exception! = Null)
{
// E. ExceptionHandled = true;
LogManager. AddActivity ("Add <Fixing Result-ELNZC>", false );
If (e. Exception. InnerException! = Null & e. Exception. InnerException is FixingDateDuplicatedException)
{
FixingDateDuplicatedException fx = e. Exception. InnerException as FixingDateDuplicatedException;
LblErrorMessage. Text = MessageManager. GetMessage (fx. MessageID, Language). MessageDesc;
}
Else
{
LblErrorMessage. Text = MessageManager. GetMessage (SysMessage. COMMON_SAVE_FAILED, Language). MessageDesc;
}
}
Else
{
LogManager. AddActivity ("Add <Fixing Result-ELNZC>, data key:" + e. ReturnValue );
}
}