Question

I'm try get all controls in my page by Page.Controls, but it is returning his Master Page control only, how to can I get this content page controls?

<%@ Page Title="" Language="C#" MasterPageFile="~/Template/ManagerTemplate.Master" AutoEventWireup="true" CodeBehind="Cliente.aspx.cs" Inherits="SistemaPedido.Cadastro.Cliente" %>

My event

protected void Page_Load(object sender, EventArgs e)
        {
            Page.LoadComplete += (s, arg) =>
            {
                foreach (Control Control in Page.Controls)
                {
                    Response.Write(Control.ClientID + "<br/>");
                }
            };
        }
Was it helpful?

Solution

List<Control> lst_controls = new List<Control>();
        public void btnClick()
        {
             RetrieveAllControls(this.Page);
             foreach(Control contrl in lst_controls)
             {
                     // all controls 
             }
        }
        public static void RetrieveAllControls(Control control)
        {
            foreach (Control ctr in control.Controls)
            {
                if (ctr != null)
                {

                    lst_controls.add(ctr);             
                    if (ctr.HasControls())
                    {
                        RetrieveAllControls(ctr, strID);

                    }
                }
            }
            return null;
        }

Maybe you can try the above :)

OTHER TIPS

It will help if you rename this way: foreach (Control control in Page.Controls) to avoid confusion. Possibly you're not seeing the controls because of this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top