Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Access to controls in footer template

Reply
Thread Tools

Access to controls in footer template

 
 
George
Guest
Posts: n/a
 
      05-11-2004
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>
 
Reply With Quote
 
 
 
 
Leonardo Rodríguez
Guest
Posts: n/a
 
      05-11-2004
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
Location : Bs.As., Argentina



"George" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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>



 
Reply With Quote
 
 
 
 
George Ceaser
Guest
Posts: n/a
 
      05-12-2004
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).Con trols.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!
 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      05-12-2004
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.

 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      05-12-2004
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.

 
Reply With Quote
 
Leonardo Rodríguez
Guest
Posts: n/a
 
      05-12-2004
Great!
Works different in the datalist i think then!

--

Leonardo C. Rodríguez
Email at: (E-Mail Removed)
Location : Bs.As., Argentina



"George Ceaser" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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).Con trols.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!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any way to reorder how a datagrid is drawn (header/footer/items vs.header/items/footer)? Henrik ASP .Net Datagrid Control 1 07-06-2006 12:23 AM
How Access Controls In A DataGrid Footer Tim Burda ASP .Net Datagrid Control 2 12-17-2004 03:28 PM
access to control in footer template =?Utf-8?B?bWFoc2E=?= ASP .Net 3 07-10-2004 05:25 PM
Method to Access Controls In A DataGrid Footer SteveFoo ASP .Net Datagrid Control 0 03-06-2004 11:10 AM
Accessing controls within a datalist footer template Paul Maidment ASP .Net 1 08-07-2003 04:39 PM



Advertisments