Re: Find footer labels in DataGrid

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: tshad (tfs_at_dslextreme.com)
Date: 11/03/04


Date: Tue, 2 Nov 2004 23:16:57 -0800


"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:ogtgo0hn50fnmag0nj4p8jnj0h4nh0t61f@4ax.com...
> Hi tshad:
>
> You might want to try during the ItemDataBound event instead (check
> for e.Item.ItemType == ListItemType.Footer).
>
> If you need to wait until afterwards, then you can index into the
> Controls collection of the grid. (The Items collection might look
> tempting at first, but it only contains DataGridItems bound to
> records, the header and footer items appear only in the Controls
> collection). The footer item should be in the last element of the
> Controls array.
>
> I have some examples about 1/2 through this article:
> http://www.odetocode.com/Articles/116.aspx

I tried some of what you had and here is what I came up with.

The routine where I need to get access to the label in the footer is:

**********************************************************************
Sub OnSelectIndexChanged(sender as Object,e as DataListCommandEventArgs)
    if e.CommandName = "Select" then
      DataList1.SelectedIndex = e.Item.ItemIndex
    end if

    Dim oGrid as DataGrid =
CType(DataList1.Items(DataList1.SelectedIndex).FindControl("DataGrid1"),Data
Grid)

    Dim lblPositionIDF as Label =
oGrid.Controls(0).Controls(0).FindControl("lblPositionIDFooter")
**********************************************************************

Gives me the following error:

Specified argument was out of the range of valid values. Parameter name:
index

The DataGrid footer section looks like:

**********************************************************************
<asp:TemplateColumn visible="true">
  <ItemTemplate>
    <asp:Label id="lblPositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
    </asp:Label>
  </ItemTemplate>
  <FooterTemplate>
    <asp:Label id="lblPositionIDFooter" runat="server">
    </asp:Label>
  </FooterTemplate>
</asp:TemplateColumn>
**********************************************************************

This doesn't seem to be it, unless I am missing something (and I probably
am).

Thanks,

Tom.

>
> HTH,
>
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>
>
>
>
> On Tue, 2 Nov 2004 17:14:06 -0800, "tshad"
> <tscheiderich@ftsolutions.com> wrote:
>
> >How do I find (and set) a couple of labels in the Footer after a DataGrid
is
> >filled?
> >
> >I have a bunch of DataGrids that get displayed nested inside a DataList.
> >
> >The datagrid looks like:
> >
>
>***************************************************************************
****
> ><asp:DataGrid visible="False"
> > border=1
> > id="DataGrid1"
> > runat="server"
> > Width="400px"
> > Height="79px"
> > AutoGenerateColumns="False"
> > GridLines="None"
> > ShowFooter="true"
> > onItemDataBound="DataGrid1_ItemDataBound"
> > OnItemCommand="DataInsert"
> > onEditCommand="DataEdit"
> > onCancelCommand="DataCancel"
> > onUpdateCommand="DataUpdate">
> > <AlternatingItemStyle BorderWidth="0px" BorderStyle="None"
> >BorderColor="White" BackColor="linen"></AlternatingItemStyle>
> > <HeaderStyle Font-Bold="True" BackColor="#6699cc"></HeaderStyle>
> > <Columns>
> > <asp:TemplateColumn HeaderText="Answer">
> > <ItemTemplate>
> > <asp:Label id="lblAnswer" runat="server" Text='<%#
> >DataBinder.Eval(Container, "DataItem.Answer") %>'>
> > </asp:Label>
> > </ItemTemplate>
> > <FooterTemplate>
> > <asp:TextBox id="txtAnswerFooter" runat="server"></asp:TextBox>
> > </FooterTemplate>
> > <EditItemTemplate>
> > <asp:textbox id="txtAnswer" runat="server" Text='<%#
> >DataBinder.Eval(Container, "DataItem.Answer") %>'>
> > </asp:textbox>
> > </EditItemTemplate>
> > </asp:templateColumn>
> > <asp:TemplateColumn >
> > <ItemTemplate>
> > <asp:Label id="lblPositionID" runat="server" Text='<%#
> >DataBinder.Eval(Container, "DataItem.PositionID") %>'>
> > </asp:Label>
> > </ItemTemplate>
> > <FooterTemplate>
> > <asp:Label id="lblPositionIDFooter" runat="server">
> > </asp:Label>
> > </FooterTemplate>
> > </asp:TemplateColumn>
> > <asp:TemplateColumn >
> > <ItemTemplate>
> > <asp:Label id="lblQuestionUnique" runat="server" Text='<%#
> >DataBinder.Eval(Container, "DataItem.QuestionUnique") %>'>
> > </asp:Label>
> > </ItemTemplate>
> > <FooterTemplate>
> > <asp:Label id="lblQuestionUniqueFooter" runat="server">
> > </asp:Label>
> > </FooterTemplate>
> > </asp:TemplateColumn>
> > <asp:BoundColumn DataField="AnswerUnique" visible="false"
ReadOnly="true"
> >HeaderText="AnswerUnique"></asp:BoundColumn>
> > <asp:EditCommandColumn EditText="<img
src='..\images\Edit.png'border='0'
> >id='textbox1'>" visible="true"
> > ButtonType="LinkButton"
> > UpdateText="<img src='..\images\update.png' border='0'>"
> >CancelText="<img src='..\images\Cancel.png' border='0'>" />
> > <asp:TemplateColumn >
> > <FooterTemplate>
> > <asp:LinkButton CommandName="Insert" Text="<img
> >src='..\images\Add.gif'border='0'>" ID="btnAdd" Runat="server" />
> > </FooterTemplate>
> > <ItemTemplate>
> > <asp:LinkButton CommandName="Delete" Text="<img
> >src='..\images\Delete.gif'border='0'>" ID="btnDel" Runat="server" />
> > </ItemTemplate>
> > </asp:TemplateColumn>
> > </Columns>
> ></asp:DataGrid>
>
>***************************************************************************
************
> >
> >I have the DataGrid by doing something like:
> >
> > for each oItem as DataListItem in DataList1.Items
> > Dim oGrid as DataGrid =
CType(oItem.FindControl("DataGrid1"),DataGrid)
> >
> >How would I use oGrid to find and set "lblPositionIDFooter" and
> >"lblQuestionUniqueFooter"?
> >
> >I need this as I need a place to put the PositionID and QuestionUnique as
> >they are keys I need to use in my Insert statement. I am inserting rows
> >from the Footer and need to get these variables from some where, so this
> >seems to be the most logical place to store them.
> >
> >Thanks,
> >
> >Tom.
> >
>


Quantcast