Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   Access to controls in footer template (http://www.velocityreviews.com/forums/t760573-access-to-controls-in-footer-template.html)

George 05-11-2004 03:52 PM

Access to controls in footer template
 
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 05-11-2004 04:20 PM

Re: Access to controls in footer template
 
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: lcrodriguez@fibertel.com.ar
Location : Bs.As., Argentina



"George" <GCeaser@aol.com> wrote in message
news:1e10c039.0405110752.6a4d2d1c@posting.google.c 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>




George Ceaser 05-12-2004 03:10 AM

Re: Access to controls in footer template
 
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!

Jeffrey Tan[MSFT] 05-12-2004 06:55 AM

RE: Access to controls in footer template
 
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] 05-12-2004 07:00 AM

Re: Access to controls in footer template
 
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.


Leonardo Rodríguez 05-12-2004 07:56 PM

Re: Access to controls in footer template
 
Great!
Works different in the datalist i think then!

--

Leonardo C. Rodríguez
Email at: lcrodriguez@fibertel.com.ar
Location : Bs.As., Argentina



"George Ceaser" <gceaser@aol.com> wrote in message
news:%232Kj368NEHA.3012@tk2msftngp13.phx.gbl...
> 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!





All times are GMT. The time now is 08:02 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.