a questo link trovare un eccellente esempio di come gestire l'eccezioni sollevate dal controllo SqlDataSource.
Analogamente è possibile gestire l'eccezioni sollevate dai controlli ObjectDataSource e LinqDataSource.
Utilizzando molto, ognuno dei suddetti controlli, mi sono trovato presto a affrontare la manutenzione di una marea di pagine contenente la logica di visualizzazione delle eccezioni.
ho deciso quindi di realizzare un WebControl che associato a un DataSourceControl potesse intercettare l'eccezioni e visualizzare.
per prima cosa ci creeremo una serie di metodi helper, per associare runtime un gestore all'evento , in particolare realizzeremo degli Extension Methods.
<p />using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using ReflectionExtension;
namespace ReflectionExtension
{
public static class ObjectReflectionExtension
{
public static void AddHanlder(this Object objectEventRaiser, object objectBoundToDelegate, string methodName, string eventName)
{
if (objectEventRaiser == null | objectBoundToDelegate == null | string.IsNullOrEmpty(methodName) | string.IsNullOrEmpty(eventName))
{
throw new ArgumentNullException();
}
else
{
Type _dataSourceType = objectEventRaiser.GetType();
EventInfo _eventInfo = _dataSourceType.GetEvent(eventName);
if (_eventInfo == null)
{
throw new ArgumentException(string.Format("Impossibile trovare l'evento {0}", eventName));
}
else
{
MethodInfo _methodInfo = objectBoundToDelegate.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
if (_eventInfo == null)
{
throw new ArgumentException(string.Format("Impossibile trovare il metodo {0}", methodName));
}
else
{
Delegate _delegate = Delegate.CreateDelegate(_eventInfo.EventHandlerType, objectBoundToDelegate, _methodInfo);
_eventInfo.AddEventHandler(objectEventRaiser, _delegate);
}
}
}
}
public static bool IsNull(this Object item)
{
return item == null;
}
}
}
namespace WebControlReflectionExtension
{
public static class WebControlReflectionExtension
{
public static void AddInsertedHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
{
ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Inserted");
}
public static void AddDeleteddHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
{
ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Deleted");
}
public static void AddUpdateddHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
{
ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Updated");
}
}
}
<p />Per inserire un commento, devi avere un account.
Fai il login e torna a questa pagina, oppure registrati alla nostra community.
- Modificare il controllo TreeView, il 2 dicembre 2008 alle 19:27
- Perfezioniamo il ProfileDataSorce, il 13 ottobre 2008 alle 11:14
- Un DataSource per il Profile, il 10 ottobre 2008 alle 15:06
- TrueSpace 7 gratuito, il 24 luglio 2008 alle 10:59
- adesso dico la mia sull'Iphone, il 22 luglio 2008 alle 10:33
- Come tutto ebbe inizio, il 21 luglio 2008 alle 09:22
