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.
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");
}
}
}
azz volevo approfittare della tua assenza
ma anche stavolta mi hai beccato
ciao marco
Aggiungi un nuovo commento »»»
Per inserire un commento, devi registrarti alla nostra community.





Stampa
Download 
10annidi.ASPItalia.com: iscriviti alla competizione e vinci fantastici premi ogni mese!
non posso allontarmi un attimo che mi sgarri alla regola numero uno...
Continua »»» | Rispondi »»»