增加新记录时ObjectDataSoruce和FormViw中的相关事件使用总结

首先,当在FormView中按下commandName="Insert"的按钮时,.NET先会执行object data source的OnInserted事件。这个事件有一个ObjectDataSourceStatusEventArgs类型的参数:它有几个属性我们会用到:

1. e.Exception --表示增加时发生的异常对象,如果在调用的目标对象上发生异常,查看InnerException对象以取得更多信息。
2. e.ExceptionHandled --表示是否将异常标记为已处理,如果设为true,异常不会传到后面的事件中去。
3. e.ReturnValue --表示你在object data source上设置的InsertMethod返回的值,可以通过此值检查是否成功。

接着,.NET会执行FormView的OnItemInserted事件,如果前面object data source的ExceptionHandled没有被设为true,那么Exception会传入这个事件中,此事件有一个FormViewInsertedEventArgss类型的参数:它有几个属性我们会用到:
1. e.ExceptionHandled --表示是否将异常标记为已处理,如果设为true,异常不会传到后面的Page.Error事件中去。否则如果定义了Page.Error事件,先执行Page.Error事件,如果Page.Error没有执行Server.ClearError()方法,异常再传入Application_Error事件中去.未定义Page.Error事件处理程序时,直接传入Application_Error事件中。
2. e.KeepInInsertMode --是否保持Insert模式中,通常如果出错,我们会显示一个错误信息,保持在Insert模式方便用户修改


    
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);
        }

    }
原文地址:https://www.cnblogs.com/rockniu/p/781615.html