|
Recursively Finding All Controls of a specified Type via Generics By Peter Bromberg Printer Friendly Version |  |
Demonstration of a static method to recursively return all controls of a specified type from an ASP.NET page. |
Recently somebody on the ASP.NET forums asked how to find all the TextBoxes on a page. There were some good answers, including one using generic List<TextBox>, but my first thought was, "What if I just want a generic method that will find all of any type of control?". So without further delay, I put together this "proof of concept".
First of all, if this is something you may need to do a lot of (changing styles, properties or whatever on all the same type of control on a page) then it should probably be made a static method. In this manner you can either put it in Global.asax.cs where it is accessible from any page, or you could put the method into a static class and accomplish much the same. So first, here's the method I came up with:
public static List<Control> GetAllControls(List<Control> controls, Type t, Control parent /* can be Page */)
{
foreach (Control c in parent.Controls)
{
if (c.GetType()== t)
controls.Add(c);
if (c.HasControls())
controls = GetAllControls(controls, t, c);
}
return controls;
}Notice that I've declared a return type of List<Control> which allows me to return any kind of ASP.NET control. The method signature needs to accept the initial List to be populated as a parameter, since the method is called recursively. We also need to pass in the Type of the control we want to get back, and we need to specify the parent control so we'll know where to start iteration. Since the Page class derives from Control, we can pass "this" as the parent parameter.
In the sample page that proofs the concept, I have some Textboxes and some CheckBoxes. One each of the TextBox and CheckBox controls are inside a Panel control, so as to correctly illustrate the recursion. The resulting List<Control> is bound to a GridView so we can look at the results. I also iterate over the returned collection and either populate its Text property, or if a CheckBox, I set its Checked property. The method to perform this looks like so:
protected void Unnamed1_Click(object sender, EventArgs e)
{
// clear everything out first to avoid confusion:
GridView1.DataSource = null;
GridView1.DataBind();
Object o = null;
if (this.DropDownList1.SelectedValue =="TextBox")
o = new TextBox();
else
o = new CheckBox();
System.Collections.Generic.List<Control> theList =Global.GetAllControls(new List<Control>(), o.GetType(), this );
this.GridView1.DataSource = theList;
GridView1.DataBind();
// do something useful to each found control...
foreach( Control c in theList)
{
if (c is TextBox) ((TextBox) c).Text = "Found " + c.ID;
if (c is CheckBox) ((CheckBox) c).Checked = true;
}
}You can download the sample Visual Studio 2008 Solution here. |
| Biography |
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking ,financial and telephony for 20 years. Pete focuses exclusively on the .NET Platform, and his samples at GotDotNet.com have been downloaded over 56,000 times. Peter enjoys producing 3D raytraced digital photo collage with Maya, the beach, and fine wines. You can view Peter's UnBlog, IttyUrl, and BlogMetafinder sites. Please post questions at forums, not via email! |  |  |
 |
| |
| Article Discussion: Recursively Finding All Controls of a specified Type via Generics |
| Peter Bromberg posted at 23-Jul-08 11:00 |
| Original Article |
 |
|
| |
| Data Binding problem |
| jessy anderson replied to Peter Bromberg at 24-Jul-08 01:07 |
good morning Mr. Peter
n have a nice day..
i've created two drop down lists the first named FieldDropDownList and is populated with data from sql database named Field, the second one named DoctorDropDownList that is populated with data from sql database named Doctor, all using sql data source control, the doctor list that appears in the swcond drop down list filtered by the user selection on the first drop down list, my problem is that i need to display the articles for the selected doctor in the second drop down list using gridview control from a data base , here is my code...pls help me solve this problem it is part in my graduation project
protected void DoctorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = @"Data Source=ENG-NAHLA\SQLEXPRESS;Initial Catalog=Journal;Integrated Security=True;Max Pool Size=200;Connect Timeout=200";
SqlConnection JournalConnection = new SqlConnection(constr);
string sqlstat = "SELECT Article.ArticleName FROM Article INNER JOIN CompinedTable ON Article.Id = CompinedTable.ArticleId AND CompinedTable.DoctorID ='" + DoctorDropDownList.SelectedItem.Value + "'";
SqlCommand sqlcmd = JournalConnection.CreateCommand();
sqlcmd.CommandText = sqlstat;
SqlDataAdapter sqldada = new SqlDataAdapter();
sqldada.SelectCommand = sqlcmd;
DataSet DS = new DataSet();
//JournalConnection.Open();
string DTN = "Articles";
sqldada.Fill(DS, DTN);
DataTable mydatatable = DS.Tables[DTN];
this.GridView1.DataSource = mydatatable;
this.DataBind();
//JournalConnection.Close();
} |
 |
|
| |
| Generally speaking, this is not how you make a post. |
| Peter Bromberg replied to jessy anderson at 24-Jul-08 09:05 |
You are posting a question as a response to an article, as opposed to making a new post in the appropriate forums topic. People will still see it, but it's better to understand the general forum "netiquette" before you post.
|
 |
|
| |
|