Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ContentPlaceHolder [modified]

ContentPlaceHolder [modified]

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdesigndata-structureshelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eslam soliman
    wrote on last edited by
    #1

    Hello Every One, i have a function that loop inside the controls inside my asp.net page and put all of this controls in an array list but This page Implement the master page which already has controls inside ,so i do not want to add these controls i want to add only the controls in the content place holders . i will appreciate it if any one can help me ,thanks in advance . Eslam Soliman Adam Attached code:

    public class MyBasePage : System.Web.UI.Page
    {

    public ArrayList controlList = new ArrayList();
    protected override void OnLoad(EventArgs e)
    {
    
        ListControlCollections();
        base.OnLoad(e);
    }
    public void ListControlCollections()
    {
        ArrayList controlList = new ArrayList();
    
        AddControls(Page.Controls, controlList);
    
        foreach (string str in controlList)
        {
            Response.Write(str + "<br/>");
        }
        Response.Write("Total Controls:" + controlList.Count);
    }
    
    public void AddControls(ControlCollection page, ArrayList controlList)
    {
        foreach (Control c in page)
        {
    
           
            if (((c.ID != null) && (c.GetType().ToString().IndexOf("WebControl") >= 0)) || (c.GetType().ToString().IndexOf("CalendarControl") >= 0 || (c.GetType().ToString().IndexOf("OrienControls") >= 0)))
            {
                controlList.Add(c.ID);
            }
    
            if (c.HasControls())
            {
                AddControls(c.Controls, controlList);
            }
        }
    }
       
    public MyBasePage()
    {
    	//
    	// TODO: Add constructor logic here
    	//
    }
    

    }

    modified on Wednesday, June 16, 2010 7:01 AM

    S K 2 Replies Last reply
    0
    • E eslam soliman

      Hello Every One, i have a function that loop inside the controls inside my asp.net page and put all of this controls in an array list but This page Implement the master page which already has controls inside ,so i do not want to add these controls i want to add only the controls in the content place holders . i will appreciate it if any one can help me ,thanks in advance . Eslam Soliman Adam Attached code:

      public class MyBasePage : System.Web.UI.Page
      {

      public ArrayList controlList = new ArrayList();
      protected override void OnLoad(EventArgs e)
      {
      
          ListControlCollections();
          base.OnLoad(e);
      }
      public void ListControlCollections()
      {
          ArrayList controlList = new ArrayList();
      
          AddControls(Page.Controls, controlList);
      
          foreach (string str in controlList)
          {
              Response.Write(str + "<br/>");
          }
          Response.Write("Total Controls:" + controlList.Count);
      }
      
      public void AddControls(ControlCollection page, ArrayList controlList)
      {
          foreach (Control c in page)
          {
      
             
              if (((c.ID != null) && (c.GetType().ToString().IndexOf("WebControl") >= 0)) || (c.GetType().ToString().IndexOf("CalendarControl") >= 0 || (c.GetType().ToString().IndexOf("OrienControls") >= 0)))
              {
                  controlList.Add(c.ID);
              }
      
              if (c.HasControls())
              {
                  AddControls(c.Controls, controlList);
              }
          }
      }
         
      public MyBasePage()
      {
      	//
      	// TODO: Add constructor logic here
      	//
      }
      

      }

      modified on Wednesday, June 16, 2010 7:01 AM

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      Inspite of people telling you repeatedly that use 'PRE' tags to format code such that your post is readable, you keep posting code without them. It's really difficult for people to go through the code without it. You can still edit your question. Use 'code block' present in the formatting options. Place your code in between those code blocks pre tag.

      E 1 Reply Last reply
      0
      • S Sandeep Mewara

        Inspite of people telling you repeatedly that use 'PRE' tags to format code such that your post is readable, you keep posting code without them. It's really difficult for people to go through the code without it. You can still edit your question. Use 'code block' present in the formatting options. Place your code in between those code blocks pre tag.

        E Offline
        E Offline
        eslam soliman
        wrote on last edited by
        #3

        Sorry It is the first time to join Forum and write code inside message ,I have already edit it to be able to read. thanks.

        1 Reply Last reply
        0
        • E eslam soliman

          Hello Every One, i have a function that loop inside the controls inside my asp.net page and put all of this controls in an array list but This page Implement the master page which already has controls inside ,so i do not want to add these controls i want to add only the controls in the content place holders . i will appreciate it if any one can help me ,thanks in advance . Eslam Soliman Adam Attached code:

          public class MyBasePage : System.Web.UI.Page
          {

          public ArrayList controlList = new ArrayList();
          protected override void OnLoad(EventArgs e)
          {
          
              ListControlCollections();
              base.OnLoad(e);
          }
          public void ListControlCollections()
          {
              ArrayList controlList = new ArrayList();
          
              AddControls(Page.Controls, controlList);
          
              foreach (string str in controlList)
              {
                  Response.Write(str + "<br/>");
              }
              Response.Write("Total Controls:" + controlList.Count);
          }
          
          public void AddControls(ControlCollection page, ArrayList controlList)
          {
              foreach (Control c in page)
              {
          
                 
                  if (((c.ID != null) && (c.GetType().ToString().IndexOf("WebControl") >= 0)) || (c.GetType().ToString().IndexOf("CalendarControl") >= 0 || (c.GetType().ToString().IndexOf("OrienControls") >= 0)))
                  {
                      controlList.Add(c.ID);
                  }
          
                  if (c.HasControls())
                  {
                      AddControls(c.Controls, controlList);
                  }
              }
          }
             
          public MyBasePage()
          {
          	//
          	// TODO: Add constructor logic here
          	//
          }
          

          }

          modified on Wednesday, June 16, 2010 7:01 AM

          K Offline
          K Offline
          KingHau
          wrote on last edited by
          #4

          The simple way is that you should replace your code [AddControls(Page.Controls, controlList);] in method ListControlCollections by the code [AddControls(Page.Master.FindControl("ContentPlaceHolder1").Controls, controlList);] In above code I assume that your ContentPlaceHolder ID is "ContentPlaceHolder1". The idea of this code is that you first search your ContentPlaceHolder then only search all controls in the searched ContentPlaceHolder control.

          Make it better.

          E 1 Reply Last reply
          0
          • K KingHau

            The simple way is that you should replace your code [AddControls(Page.Controls, controlList);] in method ListControlCollections by the code [AddControls(Page.Master.FindControl("ContentPlaceHolder1").Controls, controlList);] In above code I assume that your ContentPlaceHolder ID is "ContentPlaceHolder1". The idea of this code is that you first search your ContentPlaceHolder then only search all controls in the searched ContentPlaceHolder control.

            Make it better.

            E Offline
            E Offline
            eslam soliman
            wrote on last edited by
            #5

            The Problem is Have more than one contentplaceholder so ,I will not search for a specific One but all I need is to search only in the contentplaceholders controls without getting any controls from master page i tried your idea but it do work only with one contentplaceholder. and the out put is: ============================== Irange-------------------------> inside master page LoginStatus1------------------------->inside master page ContentPlaceHolder1------------------>Contain the following Panel1 TextBox1 Label1 Button1 ImageButton1 LinkButton1 DropDownList1 HyperLink1 ListBox1 txt_startDate CheckBox1 Calendar1 gv_List Button2 ContentPlaceHolder2--------------------->contain the following Eslam EslamTXT Total Controls:20 ============================== the idea that i want it loop only through the contentplaceholders Thanks.

            K 1 Reply Last reply
            0
            • E eslam soliman

              The Problem is Have more than one contentplaceholder so ,I will not search for a specific One but all I need is to search only in the contentplaceholders controls without getting any controls from master page i tried your idea but it do work only with one contentplaceholder. and the out put is: ============================== Irange-------------------------> inside master page LoginStatus1------------------------->inside master page ContentPlaceHolder1------------------>Contain the following Panel1 TextBox1 Label1 Button1 ImageButton1 LinkButton1 DropDownList1 HyperLink1 ListBox1 txt_startDate CheckBox1 Calendar1 gv_List Button2 ContentPlaceHolder2--------------------->contain the following Eslam EslamTXT Total Controls:20 ============================== the idea that i want it loop only through the contentplaceholders Thanks.

              K Offline
              K Offline
              KingHau
              wrote on last edited by
              #6

              Ok, I see. So I think first we should implement a method that list all ContentPlaceHolder control such as GetAllContentPlaceHolders. Then we will loop all get controls and search all inside controls. The GetAllContentPlaceHolders could be implemented as following code:

                  private IEnumerable<Control> GetAllContentPlaceHolders()
                  {
                      Queue<Control> controls = new Queue<Control>();
                      foreach (Control c in Page.Controls)
                      {
                          controls.Enqueue(c);
                      }
                      while (controls.Count > 0)
                      {
                          Control c = controls.Dequeue();
                          if (c is ContentPlaceHolder)
                          {
                              yield return c;
                          }
                          foreach (Control ic in c.Controls)
                          {
                              controls.Enqueue(ic);
                          }
                      }
                  }
              

              After that in your orginal ListControlCollections method, you should replace AddControls(Page.Controls, controlList); by

                      foreach (Control c in GetAllContentPlaceHolders())
                      {
                          AddControls(c.Controls, controlList); 
                      }
              

              Hope it helps Best regards, HauLD

              Make it better.

              E 1 Reply Last reply
              0
              • K KingHau

                Ok, I see. So I think first we should implement a method that list all ContentPlaceHolder control such as GetAllContentPlaceHolders. Then we will loop all get controls and search all inside controls. The GetAllContentPlaceHolders could be implemented as following code:

                    private IEnumerable<Control> GetAllContentPlaceHolders()
                    {
                        Queue<Control> controls = new Queue<Control>();
                        foreach (Control c in Page.Controls)
                        {
                            controls.Enqueue(c);
                        }
                        while (controls.Count > 0)
                        {
                            Control c = controls.Dequeue();
                            if (c is ContentPlaceHolder)
                            {
                                yield return c;
                            }
                            foreach (Control ic in c.Controls)
                            {
                                controls.Enqueue(ic);
                            }
                        }
                    }
                

                After that in your orginal ListControlCollections method, you should replace AddControls(Page.Controls, controlList); by

                        foreach (Control c in GetAllContentPlaceHolders())
                        {
                            AddControls(c.Controls, controlList); 
                        }
                

                Hope it helps Best regards, HauLD

                Make it better.

                E Offline
                E Offline
                eslam soliman
                wrote on last edited by
                #7

                Thanks ,I will

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups