Iterate through the controls of a ItemTemplate

  • Thread starter Joe Abou Jaoude
  • Start date
J

Joe Abou Jaoude

Hi,
I have a datagrid with boudcolums and templatecolumns. The
templatecolums have a ItemTemplate that contains controls.
I would like to iterate through these controls. I cannot use the
FindControl method because I dont know the ID of the controls. I don't
even know the type of the controls. I m writting a generic function to
iterate throuh th controls and get back with their types, ID, Text ...

Surprisingly, I didn't find something that can allow me to do that.

Thank you
 
J

Joe Abou Jaoude

Unfortunately, I m working at design time. I cannot loop through
controls collection because the collection is empty
 
E

Eliyahu Goldin

But you said you couldn't use FindControl because you didn't know the id.
How can you use FindControl in design time if you know the id?
 
T

tcarternyc

Hi,

It sounds like you want to do something similar to what I have just
written. In my example dgDupManager is a datagrid. I wanted to iterate
the grid and find the controls buried in template columns (Item,
Alternating, Selected, etc) within each row of the grid. I used an
array list to make the controls easily accessible to me. The array
list in my example is called activeControls. Since I wanted to change
the values for these select controls I thought it would be easiser to
do with an array list because the grid had many controls that I do not
care to change.

for(int i=0;i<dgDupManager.Items.Count ;i++)
if (dgDupManager.Items.HasControls())
for(int n=0;n<dgDupManager.Items.Cells.Count ;n++)
if (dgDupManager.Items.Cells[n].HasControls())
foreach (Control c in dgDupManager.Items.Cells[n].Controls)
if (c.GetType().ToString().IndexOf("DropDownList")>0 ||
c.GetType().ToString().IndexOf("Button")>0)
//add a specific type of control into my array
list
activeControls.Add(c); //defined in my Page as ArrayList


The way that I parse my Array List and make changes to the control
values can be seen in this routine (which is executed when a button is
clicked). The goal in my case was to have the button change values for
selected controls between itself and the next button. I used another
trick of the datagrid to enable me to only place the buttons into an
artificially generated sub-header for the datagrid. The
assembleActiveControls method contains only the code above. The
activeControls[x] (see below) just allows me to go through the
controls that I care about without searching the datagrid. This kind
of functionality is good when you will build a datagrid that has
dynamically created controls or whose control id's are unknown to you.

//reference clicked button
System.Web.UI.WebControls.Button _button =
(System.Web.UI.WebControls.Button) sender;

//parse dropdownlist and button controls into an arraylist
assembleActiveControls();

//find location of the clicked button and reference next controls in
arraylist
int x = activeControls.LastIndexOf(_button) + 1;

//toggle the records in the clicked subheader
while(x < activeControls.Count &&
!
activeControls[x].GetType().ToString().Equals("System.Web.UI.WebControls.Button") )
{
DropDownList _temp = (DropDownList)activeControls[x];

//toggle the active controls based on the button clicked
if (_button.Text == "Process")
_temp.SelectedIndex = 1;
if (_button.Text == "Filter")
_temp.SelectedIndex = 0;
x++;
}

Hopefully, this will help someone since I just spent a weekend
thinking about the best way to handle this question. The above is not
completely finished code but hopefully its enough so that anyone
trying to do this in the future can get a good sense of how to
address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top