ContentPlaceHolder [modified]
-
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
-
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
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.
-
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.
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.
-
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
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.
-
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.
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.
-
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.
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);
byforeach (Control c in GetAllContentPlaceHolders()) { AddControls(c.Controls, controlList); }
Hope it helps Best regards, HauLD
Make it better.
-
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);
byforeach (Control c in GetAllContentPlaceHolders()) { AddControls(c.Controls, controlList); }
Hope it helps Best regards, HauLD
Make it better.
Thanks ,I will