Dicas: Função recursiva para acessar um control

by Stiven 3. October 2009 07:47

Sei que já é bem batido, mas mesmo assim muitas pessoas me perguntam como fazer algo dinâmico para acessar um determinado control dentro de uma hierarquia vários controls. E uma boa dica é usar uma função recursiva.

public static System.Web.UI.Control FindControlRecursive(System.Web.UI.Control root, string id)

{

    if (root.ID == id)

    {

        return root;

    }

 

    foreach (System.Web.UI.Control c in root.Controls)

    {

        System.Web.UI.Control t = FindControlRecursive(c, id);

        if (t != null)

        {

            return t;

        }

    }

 

    return null;

}

Ao invês de usarmos:

Page.FindControl("control1").FindControl("control2");

usamos:

FindControlRecursive(Page, "control2");

;)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Controls | Dicas

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Sobre o autor



Meu nome é Stiven Fabiano da Câmara e sou desenvolvedor de software especializado na plataforma .NET da Microsoft, utilizando a linguagem Visual C#.