Access to controls in footer template

Discussion in 'ASP .Net Datagrid Control' started by George, May 11, 2004.

  1. George

    George Guest

    All,

    I have created a footer template which I use to add a new entry via
    my datagrid. I also have an edit template to allow the user to edit a
    row in the grid. My problem is this. The second column of the grid
    is a required field. In the edit template and footer template, i have
    included a required field validator which is linked to the
    corresponding field in appropriate template. The problem is that
    after the user selects edit (which has causesvalidation set to false)
    makes changes and selects the update button (which has the
    causesvalidation property set to true), the required field validator
    in the footer template indicates that the textbox in the footer is a
    required field.

    So, I have tried to disable the required field validator in the
    footer during the edits but cannot seem to find a way to get access to
    it. I have tried the e.Item.FindControl("xxxx") in many places but is
    seems like unless the user is adding the row, I do not have
    programmatic access to the fields in the footer template. Any ideas
    how to make this work?

    Thanks
    George

    <asp:TemplateColumn HeaderText="Division Code">
    <ItemTemplate>
    <asp:Label id=lblDivision_Code runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.Division_Code") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox id=txtidivision_code runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.Division_Code") %>'>
    </asp:TextBox>
    <asp:RequiredFieldValidator id="rfvidivisioncode"
    runat="server" ErrorMessage="Division code is required."
    Enabled="False"
    ControlToValidate="txtidivision_code">*</asp:RequiredFieldValidator>
    </FooterTemplate>
    <EditItemTemplate>
    <asp:TextBox id=txtdivision_code runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.Division_Code") %>'>
    </asp:TextBox>
    <asp:RequiredFieldValidator id="rfvdivisioncode" runat="server"
    ErrorMessage="Division code is required."
    ControlToValidate="txtdivision_code">*</asp:RequiredFieldValidator>
    </EditItemTemplate>
    </asp:TemplateColumn>
    George, May 11, 2004
    #1
    1. Advertising

  2. RequiredFieldValidator rfv = (RequiredFieldValidator)
    Grid.Controls[Grid.Controls.Count -1].FindControl("MyReqFieldVal");

    The footer should be the last control of the grid.
    It's not in the items collection, don't ask me why.
    I'll tried this with a datalist, try with the grid and see what happens.

    Regards,
    --

    Leonardo C. Rodríguez
    Email at:
    Location : Bs.As., Argentina



    "George" <> wrote in message
    news:...
    > All,
    >
    > I have created a footer template which I use to add a new entry via
    > my datagrid. I also have an edit template to allow the user to edit a
    > row in the grid. My problem is this. The second column of the grid
    > is a required field. In the edit template and footer template, i have
    > included a required field validator which is linked to the
    > corresponding field in appropriate template. The problem is that
    > after the user selects edit (which has causesvalidation set to false)
    > makes changes and selects the update button (which has the
    > causesvalidation property set to true), the required field validator
    > in the footer template indicates that the textbox in the footer is a
    > required field.
    >
    > So, I have tried to disable the required field validator in the
    > footer during the edits but cannot seem to find a way to get access to
    > it. I have tried the e.Item.FindControl("xxxx") in many places but is
    > seems like unless the user is adding the row, I do not have
    > programmatic access to the fields in the footer template. Any ideas
    > how to make this work?
    >
    > Thanks
    > George
    >
    > <asp:TemplateColumn HeaderText="Division Code">
    > <ItemTemplate>
    > <asp:Label id=lblDivision_Code runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.Division_Code") %>'>
    > </asp:Label>
    > </ItemTemplate>
    > <FooterTemplate>
    > <asp:TextBox id=txtidivision_code runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.Division_Code") %>'>
    > </asp:TextBox>
    > <asp:RequiredFieldValidator id="rfvidivisioncode"
    > runat="server" ErrorMessage="Division code is required."
    > Enabled="False"
    > ControlToValidate="txtidivision_code">*</asp:RequiredFieldValidator>
    > </FooterTemplate>
    > <EditItemTemplate>
    > <asp:TextBox id=txtdivision_code runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.Division_Code") %>'>
    > </asp:TextBox>
    > <asp:RequiredFieldValidator id="rfvdivisioncode" runat="server"
    > ErrorMessage="Division code is required."
    > ControlToValidate="txtdivision_code">*</asp:RequiredFieldValidator>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    Leonardo Rodríguez, May 11, 2004
    #2
    1. Advertising

  3. Leonardo ,

    This did not work. My controls collection only had one item in it
    and I could not even find the id it listed in my client html. After
    some messing around nesting controls collections within control
    collections I finally got this work:


    CType(dgdata.Controls(0).Controls(dgdata.Controls(0).Controls.Count -
    1).Controls(2).FindControl("rfvidivisioncode"),
    RequiredFieldValidator).Enabled = pb_Enabled


    dgdata.Controls(0).Controls(dgdata.Controls(0).Controls.Count - 1) -
    seems to return a reference to the last row (maybe?? )

    the .Controls(2). points to the third column in my grid which is the
    column which holds that required field validator but I believe this can
    be anything once you get to the footer row..... I say this because I
    also use the findcontrol on controls(2) to get a reference to other
    fields in the footer so I can disable them when an edit is being done.

    FindControl("rfvidivisioncode") - appears to find the control anywhere
    in the footer....

    I hope this helps anyone who was struggling with this also.

    George




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    George Ceaser, May 12, 2004
    #3
  4. Hi George,

    Based on my understanding, you want to access footer template in certain
    button's click event.

    Yes, if you view DataGrid.Items property in MSDN, you will see:
    "Only items bound to the data source are contained in the Items collection.
    The header, footer, and separator are not included in the collection."

    So you can not access it through Items property. But because the footer
    template is also the child control of DataGrid, you can access it through
    DataGrid's control collection.

    First, you can open the web page's trace when debugging, then you can view
    the web page's control hiberarchy, like this:

    DataGrid1 System.Web.UI.WebControls.DataGrid
    DataGrid1:_ctl0 System.Web.UI.WebControls.DataGridTable
    DataGrid1:_ctl1 System.Web.UI.WebControls.DataGridItem
    DataGrid1:_ctl1:_ctl0
    System.Web.UI.WebControls.TableCell
    DataGrid1:_ctl1:_ctl1
    System.Web.UI.WebControls.TableCell
    DataGrid1:_ctl1:_ctl2
    System.Web.UI.WebControls.TableCell
    DataGrid1:_ctl1:_ctl3
    System.Web.UI.WebControls.TableCell
    DataGrid1:_ctl1:_ctl4
    System.Web.UI.WebControls.TableCell
    DataGrid1:_ctl2 System.Web.UI.WebControls.DataGridItem
    DataGrid1:_ctl2:_ctl0
    System.Web.UI.WebControls.TableCell
    ...............

    In it, you will see DataGrid's Controls property has only one control:
    DataGrid1:_ctl0 System.Web.UI.WebControls.DataGridTable, while the Table
    class contains all the DataGridItem(s) of DataGrid. That DataGridItem is
    the row of datagrid, including Footer Template row.

    So you can access your control in datagrid like this:

    DataGridItem
    dgi=this.DataGrid1.Controls[0].Controls[this.DataGrid1.Controls[0].Controls.
    Count-1] as DataGridItem;
    if(dgi!=null)
    {
    Button bt =dgi.FindControl("Button1") as Button;
    bt.Text="Find it";
    }

    In the sample code snippet, I placed a Button in the Footer Template.

    =======================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], May 12, 2004
    #4
  5. Hi George,

    Oh, sorry for my late reply.

    I did not refresh my tool, and did not see that you have resolved your
    issue yourself. Anyway, I am glad our reply uses the same solution :)

    If you need further help, please feel free to post.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], May 12, 2004
    #5
  6. Great!
    Works different in the datalist i think then!

    --

    Leonardo C. Rodríguez
    Email at:
    Location : Bs.As., Argentina



    "George Ceaser" <> wrote in message
    news:%...
    > Leonardo ,
    >
    > This did not work. My controls collection only had one item in it
    > and I could not even find the id it listed in my client html. After
    > some messing around nesting controls collections within control
    > collections I finally got this work:
    >
    >
    > CType(dgdata.Controls(0).Controls(dgdata.Controls(0).Controls.Count -
    > 1).Controls(2).FindControl("rfvidivisioncode"),
    > RequiredFieldValidator).Enabled = pb_Enabled
    >
    >
    > dgdata.Controls(0).Controls(dgdata.Controls(0).Controls.Count - 1) -
    > seems to return a reference to the last row (maybe?? )
    >
    > the .Controls(2). points to the third column in my grid which is the
    > column which holds that required field validator but I believe this can
    > be anything once you get to the footer row..... I say this because I
    > also use the findcontrol on controls(2) to get a reference to other
    > fields in the footer so I can disable them when an edit is being done.
    >
    > FindControl("rfvidivisioncode") - appears to find the control anywhere
    > in the footer....
    >
    > I hope this helps anyone who was struggling with this also.
    >
    > George
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Leonardo Rodríguez, May 12, 2004
    #6
    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. Paul Maidment
    Replies:
    1
    Views:
    4,371
    Nick Berardi
    Aug 7, 2003
  2. =?Utf-8?B?bWFoc2E=?=

    access to control in footer template

    =?Utf-8?B?bWFoc2E=?=, Jul 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    5,278
    =?Utf-8?B?bWFoc2E=?=
    Jul 10, 2004
  3. Tim Burda

    How Access Controls In A DataGrid Footer

    Tim Burda, Jul 8, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    136
    TiSch
    Dec 17, 2004
  4. SteveFoo

    Method to Access Controls In A DataGrid Footer

    SteveFoo, Mar 6, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    123
    SteveFoo
    Mar 6, 2004
  5. Henrik
    Replies:
    1
    Views:
    417
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
Loading...

Share This Page