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

    *** Sent via Developersdex http://www.developersdex.com ***
    Joe Abou Jaoude, Jun 3, 2007
    #1
    1. Advertising

  2. You need to loop through the Controls collection.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Joe Abou Jaoude" <> wrote in message
    news:...
    >
    >
    > 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
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    Eliyahu Goldin, Jun 3, 2007
    #2
    1. Advertising

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

    *** Sent via Developersdex http://www.developersdex.com ***
    Joe Abou Jaoude, Jun 3, 2007
    #3
  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,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "Joe Abou Jaoude" <> wrote in message
    news:%...
    >
    >
    > Unfortunately, I m working at design time. I cannot loop through
    > controls collection because the collection is empty
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    Eliyahu Goldin, Jun 3, 2007
    #4
  5. Joe Abou Jaoude

    Guest

    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.

    On Jun 3, 8:40 am, Joe Abou Jaoude <> wrote:
    > Hi,
    > I have adatagridwith boudcolums and templatecolumns. The
    > templatecolums have a ItemTemplate that contains controls.
    > I would like toiteratethrough 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 toiteratethrouh 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
    >
    > *** Sent via Developersdexhttp://www.developersdex.com***
    , Jun 18, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    5,318
    Joe Fallon
    Sep 24, 2004
  2. =?Utf-8?B?T2xlZw==?=
    Replies:
    2
    Views:
    606
    =?Utf-8?B?dmluYXk=?=
    Jul 27, 2005
  3. Gogo
    Replies:
    1
    Views:
    2,085
    Sudsy
    Sep 4, 2003
  4. runescience
    Replies:
    0
    Views:
    1,442
    runescience
    Feb 9, 2006
  5. John
    Replies:
    4
    Views:
    898
    RedGrittyBrick
    Apr 1, 2008
Loading...

Share This Page