<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BeaverBlog &#187; findcontrol</title>
	<atom:link href="http://blog.crazybeavers.se/index.php/archive/tag/findcontrol/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.crazybeavers.se</link>
	<description>A beaver bloging about webdevelopment, XML and .Net</description>
	<lastBuildDate>Tue, 26 Jul 2011 10:59:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Recursive generic extensionmethods are fun! (Recursive FindControl)</title>
		<link>http://blog.crazybeavers.se/index.php/archive/recursive-generic-extensionmethods-are-fun/</link>
		<comments>http://blog.crazybeavers.se/index.php/archive/recursive-generic-extensionmethods-are-fun/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 21:39:16 +0000</pubDate>
		<dc:creator>Karl</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Code samples]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[extensionmethods]]></category>
		<category><![CDATA[findcontrol]]></category>
		<category><![CDATA[genererics]]></category>
		<category><![CDATA[recursive]]></category>

		<guid isPermaLink="false">http://www.beaverblogg.com/index.php/archive/recursive-generic-extensionmethods-are-fun/</guid>
		<description><![CDATA[Just thought I should share a little piece of code with you this evening. It&#8217;s a little extension method (i.e it requires .Net 3.5) that extends Control and helps you find all instances of a certain controltype under the Control on which you invoke the method. Just put the following code in an assembly (or [...]]]></description>
			<content:encoded><![CDATA[<p>Just thought I should share a little piece of code with you this evening. It&#8217;s a little extension method (i.e it requires .Net 3.5) that extends Control and helps you find all instances of a certain controltype under the Control on which you invoke the method. Just put the following code in an assembly (or a .cs-file in App_Code).</p>
<pre class="brush: csharp; title: ;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

/// &lt;summary&gt;
/// Holderclass for extension methods
/// &lt;/summary&gt;
public static class ExtensionMethods
{
    /// &lt;summary&gt;
    /// Recursively searchs for all instances of a specified control type.
    /// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;T&quot;&gt;The type of control to search for.&lt;/typeparam&gt;
    /// &lt;param name=&quot;control&quot;&gt;The control to search from.&lt;/param&gt;
    /// &lt;returns&gt;A List&lt;&gt; with the found controls.&lt;/returns&gt;
    public static List&lt;T&gt; FindControls&lt;T&gt;(this Control control) where T : Control
    {
        List&lt;T&gt; myList = new List&lt;t&gt;();
        FindControls&lt;/t&gt;&lt;t&gt;(control, ref myList);
        return myList;
    }

    private static void FindControls&lt;T&gt;(Control control, ref List&lt;T&gt; controlList) where T : Control
    {
        for (int i = 0; i &lt; control.Controls.Count; i++)
        {
            Control currentControl = control.Controls[i];
            if (currentControl is T)
            {
                T typedControl = control.Controls[i] as T;
                controlList.Add(typedControl);
            }

            FindControls&lt;T&gt;(currentControl, ref controlList);
        }

    }
}
</pre>
<p>To call it you just invoke FindControls&lt;Control&gt;() on any object that derives from Control (i.e. Page, Label, Calendar, the list can go on forever), see the sample below. Hope you find some use for it!</p>
<pre class="brush: csharp; title: ;">
List&lt;HtmlMeta&gt; meta = Page.FindControls&lt;HtmlMeta&gt;();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.crazybeavers.se/index.php/archive/recursive-generic-extensionmethods-are-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

