How Access Controls In A DataGrid Footer

Discussion in 'ASP .Net Datagrid Control' started by Tim Burda, Jul 8, 2003.

  1. Tim Burda

    Tim Burda Guest

    OK - I am prepared for everyone who will tell me this is a hack and
    bad idea; however, this is the only way I could find to solve this
    problem, and it seems like other people are having similiar problems,
    so I thought I would post my solution anyway.

    I have a DataGrid with controls (TextBox, DropDownList, etc.) in the
    Footer. I am using the Footer as entry area for new records. I need to
    enable/disable controls in the Footer based on the state of a dropdown
    list control (in the Footer). It appears that you can only access the
    "Footer" item in the DataGrid in the DataGrid events "ItemCreated" &
    "ItemDataBound".

    The ItemDataBound event fires only when a DataGrid.DataBind() method
    is invoked. This is not always possible as I want to re-bind the data
    only when necessary, and not always when the value in the DropDownList
    changes

    The ItemCreated event fires each item the page is loaded and items are
    created in the datagrid; however, the controls in the footer don't
    seem to be created when this event is fired.

    But, what you can you with the ItemCreated event is this:

    delcare a variable (of page level scope):

    private DataGridItem mitmFooter = null;

    trap the ItemCreated event and then do the following:

    if (e.Item.ItemType == ListItemType.Footer)
    {
    mitmFooter = e.Item;
    }

    With this reference, you can access the footer in other areas of the
    code. In my case, I use the variable mitmFooter in the
    "SelectedIndexChange" event for the dropdown list in the footer with
    code similiar to this:

    ddlSegmentType = (DropDownList)
    mitmFooter.FindControl("ddlSegmentTypeFooter");

    Word of caution: In order for this technique to be successful, you
    need to verify that the order the events are firing in are correct
    (i.e. the ItemCreated event fires before your event).

    If you need more info or have questions, my email is posted.

    Happy Coding -

    Tim Burda
    Tim Burda, Jul 8, 2003
    #1
    1. Advertising

  2. Tim Burda

    Craig Grell Guest

    Thank you

    This is just what I was looking for and so far no problems.



    (Tim Burda) wrote in
    news::

    > OK - I am prepared for everyone who will tell me this is a hack and
    > bad idea; however, this is the only way I could find to solve this
    > problem, and it seems like other people are having similiar problems,
    > so I thought I would post my solution anyway.
    >
    > I have a DataGrid with controls (TextBox, DropDownList, etc.) in the
    > Footer. I am using the Footer as entry area for new records. I need to
    > enable/disable controls in the Footer based on the state of a dropdown
    > list control (in the Footer). It appears that you can only access the
    > "Footer" item in the DataGrid in the DataGrid events "ItemCreated" &
    > "ItemDataBound".
    >
    > The ItemDataBound event fires only when a DataGrid.DataBind() method
    > is invoked. This is not always possible as I want to re-bind the data
    > only when necessary, and not always when the value in the DropDownList
    > changes
    >
    > The ItemCreated event fires each item the page is loaded and items are
    > created in the datagrid; however, the controls in the footer don't
    > seem to be created when this event is fired.
    >
    > But, what you can you with the ItemCreated event is this:
    >
    > delcare a variable (of page level scope):
    >
    > private DataGridItem mitmFooter = null;
    >
    > trap the ItemCreated event and then do the following:
    >
    > if (e.Item.ItemType == ListItemType.Footer)
    > {
    > mitmFooter = e.Item;
    > }
    >
    > With this reference, you can access the footer in other areas of the
    > code. In my case, I use the variable mitmFooter in the
    > "SelectedIndexChange" event for the dropdown list in the footer with
    > code similiar to this:
    >
    > ddlSegmentType = (DropDownList)
    > mitmFooter.FindControl("ddlSegmentTypeFooter");
    >
    > Word of caution: In order for this technique to be successful, you
    > need to verify that the order the events are firing in are correct
    > (i.e. the ItemCreated event fires before your event).
    >
    > If you need more info or have questions, my email is posted.
    >
    > Happy Coding -
    >
    > Tim Burda
    Craig Grell, Jul 9, 2003
    #2
    1. Advertising

  3. Tim Burda

    TiSch Guest

    Tim Burda wrote:
    > OK - I am prepared for everyone who will tell me this is a hack and
    > bad idea; however, this is the only way I could find to solve this
    > problem, and it seems like other people are having similiar

    problems,
    > so I thought I would post my solution anyway.
    >
    > I have a DataGrid with controls (TextBox, DropDownList, etc.) in the
    > Footer. I am using the Footer as entry area for new records. I need

    to
    > enable/disable controls in the Footer based on the state of a

    dropdown
    > list control (in the Footer). It appears that you can only access the
    > "Footer" item in the DataGrid in the DataGrid events "ItemCreated" &
    > "ItemDataBound".
    >
    > The ItemDataBound event fires only when a DataGrid.DataBind() method
    > is invoked. This is not always possible as I want to re-bind the data
    > only when necessary, and not always when the value in the

    DropDownList
    > changes
    >
    > The ItemCreated event fires each item the page is loaded and items

    are
    > created in the datagrid; however, the controls in the footer don't
    > seem to be created when this event is fired.
    >
    > But, what you can you with the ItemCreated event is this:
    >
    > delcare a variable (of page level scope):
    >
    > private DataGridItem mitmFooter = null;
    >
    > trap the ItemCreated event and then do the following:
    >
    > if (e.Item.ItemType == ListItemType.Footer)
    > {
    > mitmFooter = e.Item;
    > }
    >
    > With this reference, you can access the footer in other areas of the
    > code. In my case, I use the variable mitmFooter in the
    > "SelectedIndexChange" event for the dropdown list in the footer with
    > code similiar to this:
    >
    > ddlSegmentType = (DropDownList)
    > mitmFooter.FindControl("ddlSegmentTypeFooter");
    >
    > Word of caution: In order for this technique to be successful, you
    > need to verify that the order the events are firing in are correct
    > (i.e. the ItemCreated event fires before your event).
    >
    > If you need more info or have questions, my email is posted.
    >
    > Happy Coding -
    >
    > Tim Burda




    Hello,

    there is another way to access the footer:

    Public Shared Function getFooter(ByVal grid As DataGrid) As
    DataGridItem
    Dim footer As Control
    For Each ctrl As WebControl In grid.Controls(0).Controls
    'loop DataGridTable
    If TypeOf ctrl Is System.Web.UI.WebControls.DataGridItem
    Then
    Dim item As DataGridItem = DirectCast(ctrl,
    DataGridItem)
    Select Case item.ItemType
    Case ListItemType.Footer
    Return item
    End Select
    End If
    Next
    End Function

    Regards,
    Tim
    TiSch, Dec 17, 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. Nofear

    A solution to finding controls in datagrid footer...

    Nofear, Aug 11, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    229
  2. SteveFoo

    Method to Access Controls In A DataGrid Footer

    SteveFoo, Mar 6, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    122
    SteveFoo
    Mar 6, 2004
  3. George

    Access to controls in footer template

    George, May 11, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    5
    Views:
    216
    Leonardo Rodríguez
    May 12, 2004
  4. Henrik
    Replies:
    1
    Views:
    416
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
  5. Replies:
    0
    Views:
    147
Loading...

Share This Page