コード例 #1
0
        /// <summary>
        ///     Retrieves a control from the specified collection by Name.
        /// </summary>
        /// <typeparam name="TControl"></typeparam>
        /// <param name="controls"></param>
        /// <param name="controlName">
        ///     The name of the control which distinguishes it from other controls in the supplied
        ///     collection. The parent name prefix is not required if the controls in the collection have the same parent.
        /// </param>
        /// <param name="searchDepth">The depth of search to perform.</param>
        /// <returns></returns>
        public static TControl GetByName <TControl>(this IEnumerable <Control> controls, string controlName, SearchDepthSpecifier searchDepth = SearchDepthSpecifier.IncludeChildControls)
            where TControl : System.Web.UI.Control
        {
            List <Control> matchingControls;

            if (searchDepth == SearchDepthSpecifier.IncludeChildControls)
            {
                matchingControls = new List <Control>();

                GetByNameRecursive <TControl>(controls, controlName, matchingControls);
            }
            else
            {
                matchingControls = controls.ToList();
            }

            matchingControls = matchingControls.Where(x => x.ID.Equals(controlName) || x.ID.EndsWith("_" + controlName)).ToList();

            if (matchingControls.Count == 0)
            {
                throw new Exception(string.Format("Control Name \"{0}\" could not be found.", controlName));
            }
            if (matchingControls.Count > 1)
            {
                throw new Exception(string.Format("Control Name \"{0}\" is not unique in this collection.", controlName));
            }

            object control = matchingControls.First() as TControl;

            if (control == null)
            {
                throw new Exception(string.Format("Control Name \"{0}\" does not match specified Type \"{1}\".", controlName, typeof(TControl).Name));
            }

            return((TControl)control);
        }
コード例 #2
0
ファイル: WebControlHelper.cs プロジェクト: waldo2590/Rock
        /// <summary>
        ///     Retrieves a control from the specified collection by Name.
        /// </summary>
        /// <param name="controls"></param>
        /// <param name="controlName">
        ///     The name of the control which distinguishes it from other controls in the supplied
        ///     collection. The parent name prefix is not required if the controls in the collection have the same parent.
        /// </param>
        /// <param name="searchDepth">The depth of search to perform.</param>
        /// <returns></returns>
        public static System.Web.UI.Control GetByName(this IEnumerable <Control> controls, string controlName, SearchDepthSpecifier searchDepth = SearchDepthSpecifier.IncludeChildControls)
        {
            List <Control> matchingControls;

            if (searchDepth == SearchDepthSpecifier.IncludeChildControls)
            {
                matchingControls = new List <Control>();

                GetByNameRecursive(controls, controlName, matchingControls);
            }
            else
            {
                matchingControls = controls.ToList();
            }

            matchingControls = matchingControls.Where(x => x.ID.Equals(controlName) || x.ID.EndsWith("_" + controlName)).ToList();

            if (matchingControls.Count == 0)
            {
                throw new Exception(string.Format("Control Name \"{0}\" could not be found.", controlName));
            }
            if (matchingControls.Count > 1)
            {
                throw new Exception(string.Format("Control Name \"{0}\" is not unique in this collection.", controlName));
            }

            var control = matchingControls.First() as System.Web.UI.Control;

            return(control);
        }