Access to controls in footer template

G

George

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>
 
L

Leonardo Rodríguez

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,
 
G

George Ceaser

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
 
J

Jeffrey Tan[MSFT]

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.
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top