Controls Hierarchy in DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Kubuli John, Apr 29, 2004.

  1. Kubuli John

    Kubuli John Guest

    It appears that there are only 2 ways to get to a particular control
    in a DataGrid - either use the FindControl method or try to work your
    way through the (apparently undocumented) hierarchy of controls
    (dgItem.Controls[0].Controls[0].Controls[2]...).

    Using FindControl is OK except that to make generic sort buttons in
    the headers of template columns, I have to add LinkButtons. If I use
    FindControl to get to these controls (to enable or disable them when
    switching between view and edit modes), they each have to have a
    unique name. This means that I have to hard-code the control id's
    into my code, making it difficult to reuse the code when I change what
    the grid is displaying. Ditto for the images since I have to hide all
    other images when I show one.

    I was hoping to be able to iterate through all of the controls in the
    header row looking for LinkButtons called "btnSort", but I do not want
    to try to decipher the hierarchy of controls to use
    dgItem.Controls[0].Controls[1]... It doesn't look like there is a
    single Controls collection that goes through all of the control
    hierarchy either.

    The Columns collection is no use either since it only gives me access
    to the generic header style items, not my custom header controls.

    Is there a documented hierarchy that I can rely on to locate the
    appropriate control, or some other way to navigate through a DataGrid
    or DataGridItem?

    Thanks,

    John H.
     
    Kubuli John, Apr 29, 2004
    #1
    1. Advertising

  2. Kubuli John

    Rick Spiewak Guest

    Here's what I would recommend:
    In the ItemCreated event:

    Dim dg As DataGrid = DirectCast(sender, DataGrid)
    Dim itm As DataGridItem = e.Item
    Select Case itm.ItemType
    ..
    ..
    ..
    Case ListItemType.Header
    For i As Integer = 0 To itm.Cells(0).Controls.Count - 1
    Dim c As Control = itm.Cells(0).Controls(i)

    If c.GetType.ToString =
    "System.Web.UI.WebControls.DataGridLinkButton" Then
    dglb = DirectCast(c, LinkButton)

    ' Your code here
    ..
    ..
    ..
    Case Else
    Exit Sub
    End Select
    End Sub

    For i As Integer = 0 To itm.Cells(0).Controls.Count - 1
    Dim c As Control = itm.Cells(0).Controls(i)

    If c.GetType.ToString =
    "System.Web.UI.WebControls.DataGridLinkButton" Then
    dglb = DirectCast(c, LinkButton)


    "Kubuli John" <> wrote in message
    news:...
    > It appears that there are only 2 ways to get to a particular control
    > in a DataGrid - either use the FindControl method or try to work your
    > way through the (apparently undocumented) hierarchy of controls
    > (dgItem.Controls[0].Controls[0].Controls[2]...).
    >
    > Using FindControl is OK except that to make generic sort buttons in
    > the headers of template columns, I have to add LinkButtons. If I use
    > FindControl to get to these controls (to enable or disable them when
    > switching between view and edit modes), they each have to have a
    > unique name. This means that I have to hard-code the control id's
    > into my code, making it difficult to reuse the code when I change what
    > the grid is displaying. Ditto for the images since I have to hide all
    > other images when I show one.
    >
    > I was hoping to be able to iterate through all of the controls in the
    > header row looking for LinkButtons called "btnSort", but I do not want
    > to try to decipher the hierarchy of controls to use
    > dgItem.Controls[0].Controls[1]... It doesn't look like there is a
    > single Controls collection that goes through all of the control
    > hierarchy either.
    >
    > The Columns collection is no use either since it only gives me access
    > to the generic header style items, not my custom header controls.
    >
    > Is there a documented hierarchy that I can rely on to locate the
    > appropriate control, or some other way to navigate through a DataGrid
    > or DataGridItem?
    >
    > Thanks,
    >
    > John H.
     
    Rick Spiewak, May 3, 2004
    #2
    1. Advertising

  3. Thanks for the reply. I was trying to add the little up and down sort
    indicators in the headers and eded up just naming the images imgSortUp0,
    imgSortUp2, etc. and imgSortDn0, imgSortDn1, ...

    Then I was able to use the FindControl method to turn all of the images
    on or off - I just had to remember to change the training number in each
    column header when I pasted the images in.

    John H.

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    John Haasbeek, May 7, 2004
    #3
    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. Alan Pretre

    Re: hierarchy chart

    Alan Pretre, Jul 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,293
    Alan Pretre
    Jul 25, 2003
  2. =?Utf-8?B?SXJmYW4gQWtyYW0=?=

    Dynamic Controls in asp.net...Building a tree hierarchy..please he

    =?Utf-8?B?SXJmYW4gQWtyYW0=?=, Dec 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    522
    =?Utf-8?B?SXJmYW4gQWtyYW0=?=
    Dec 13, 2004
  3. Nathan Sokalski
    Replies:
    4
    Views:
    1,170
    Muhammad Naveed Yaseen
    Sep 5, 2007
  4. Nenad Prekupec

    How to Change hierarchy of controls?

    Nenad Prekupec, Nov 18, 2003, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    165
    Nenad Prekupec
    Nov 19, 2003
  5. Irfan Akram
    Replies:
    1
    Views:
    171
    Mujtaba Syed
    Dec 13, 2004
Loading...

Share This Page