Iterate through the controls of a ItemTemplate

Discussion in 'ASP .Net Datagrid Control' started by Joe Abou Jaoude, Jun 3, 2007.

  1. 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
    Joe Abou Jaoude, Jun 3, 2007
    1. Advertisements

  2. Eliyahu Goldin, Jun 3, 2007
    1. Advertisements

  3. Unfortunately, I m working at design time. I cannot loop through
    controls collection because the collection is empty
    Joe Abou Jaoude, Jun 3, 2007
  4. 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?
    Eliyahu Goldin, Jun 3, 2007
  5. Joe Abou Jaoude

    tcarternyc Guest


    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 ||
    //add a specific type of control into my array
    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

    //find location of the clicked button and reference next controls in
    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;

    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
    tcarternyc, Jun 18, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.