Asp.net jQuery like Closest()

Here is a very simple asp.net .Closest() function like the jquery one.

	public static class ControlExtensions
	{
		public static Control Closest(this Control control, Type type ){
			Control closest = null;
			Control parent = control.Parent;
			while(closest == null && parent != null){
				if(parent.GetType().ToString() == type.ToString()){
					return parent;
				}
				parent = parent.Parent;
			}
			return null;
		}
	}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.