[英]How to catch exception after DataBind event?
我有下一個代碼:
protected void Page_Load(object sender, EventArgs e)
{
// some code-------------------
sourceDetails.SelectCommand += "<new condition>";
this.DataBind();
}
sourceDetails
是.aspx頁中的SqlDataSource
對象,具有對數據庫的查詢。
"<new condition>"
-變化的條件。
如果條件不正確,則會在頁面上顯示錯誤。 我想在錯誤出現之前將其捕獲。
這將警告錯誤:
try
{
// some code-------------------
sourceDetails.SelectCommand += "<new condition>";
this.DataBind();
}
catch (SqlException e)
{
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + e.Message + "');", true);
}
如您在ASP.NET頁面生命周期概述文章中所看到的,有一些數據綁定處理的特定事件 。 也許您可以使用這些生命周期掛鈎( DataBinding事件或DataBound事件 )來防止錯誤,甚至可以更好地處理錯誤。
希望我能幫上忙!
感謝您對我的問題的關注! 我找到了下一個解決方案:1. SqlDataSource具有事件OnSelected。 我用過了。 2.我在OnSelected事件中編寫了以下代碼:
protected void sourceDetails_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
//Doing what we need to do
}
}
SqlDataSourceStatusEventArgs
包含我們用於緩存和處理異常所需的所有數據: Exception
和Command
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.