[英]How to catch exception occured in DAL of ObjectDataSource object?
我的asp.net Web項目的頁面之一上有ObjectDataSource和GridView。
要初始化ObjectDataSource,我使用Init事件:
protected void ObjectDataSource1_Init(object sender, EventArgs e)
{
ObjectDataSource1.EnablePaging = true;
ObjectDataSource1.TypeName = "CustomerDataSource";
ObjectDataSource1.DataObjectTypeName = "Customer";
ObjectDataSource1.SelectMethod = "GetCustomers";
ObjectDataSource1.UpdateMethod = "Update";
ObjectDataSource1.SelectCountMethod = "GetCustomersCount";
ObjectDataSource1.MaximumRowsParameterName = "maximumRows";
ObjectDataSource1.StartRowIndexParameterName = "startRowIndex";
}
DAL中的某處:
public static List<Customer> GetCustomers(int maximumRows, int startRowIndex)
{
try {
... some select to database here...
}
catch (Exception ex)
{
Utils.LogError(ex);
throw ex;
}
}
現在,讓我們想象一下,由於某種原因,GetCustomers會引發異常。 我如何捕獲此異常並進行處理? 我知道我可以使用Application_Error,但是我想以某種方式在頁面上捕獲此異常並向用戶顯示一些友好的消息。 那可能嗎?
謝謝
您可以嘗試處理相關的ObjectDataSource
事件。
在您的示例中,為Selected
事件,但還存在以相同方式處理的Inserted
, Updated
和Deleted
事件。
這些事件提供了一個ObjectDataSourceStatusEventArgs
,該對象具有Exception
,並且可以設置一個屬性ExceptionHandled
,以指示已處理了該異常(例如,顯示錯誤消息)。
MSDN中有一些示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.