My DataBinderHelper class.

Previously I’ve posted a few Helper Classes . This post describes my DataBinderHelper class.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;

    public class DataBinderHelper
    {
        public static bool IsPropertyExist(object container, string propName)
        {
            if (container == null)
            {
                throw new ArgumentNullException(“container”);
            }
            if (string.IsNullOrEmpty(propName))
            {
                throw new ArgumentNullException(“propName”);
            }
            PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties(container).Find(propName, true);
            if (descriptor1 == null)
            {
                return false;
            }
            return true;
        }
/// <summary>
///
/// </summary>
/// <param name=”container”></param>
/// <param name=”propName”></param>
/// <example>DataBinderHelper.EvalWithDefault(DataItem, “VPath”, “”)</example>
/// <param name=”defaultValue”></param>
/// <returns></returns>
        public static object EvalWithDefault(object container, string propName,object defaultValue)
        {
            object oRet=defaultValue;
            if (IsPropertyExist( container,  propName))
            {
                oRet = DataBinder.Eval(container, propName);
            }
            return oRet;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name=”container”></param>
        /// <param name=”propName”></param>
        /// <param name=”format”></param>
        /// <param name=”defaultValue”></param>
        /// <example>DataBinderHelper.EvalWithDefault(Container.DataItem,”CreationDateTime”,”{0:d}”,””)%</example>
        /// <returns></returns>
        public static object EvalWithDefault(object container, string propName, string format, object defaultValue)
        {
            object oRet = defaultValue;
            if (IsPropertyExist(container, propName))
            {
                oRet = DataBinder.Eval(container, propName, format);
            }
            return oRet;
        }
    }


Advertisements