p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=554)
-   -   FindControl() doesn't find control in a detailsView (http://p2p.wrox.com/showthread.php?t=81939)

Arcy December 16th, 2010 08:53 AM

FindControl() doesn't find control in a detailsView
 
Hi,
I am trying to set the focus on a textbox in a detailsView when its current mode is Insert or Edit.
Here is the template field in the .aspx file:
Code:

                                        <asp:TemplateField HeaderText="<%$ Resources:Tabelas, Cadeiras.NomeCadeira %>" SortExpression="NomeCadeira">
                                            <EditItemTemplate>
                                                <asp:TextBox ID="NomeCadeira" runat="server" Text='<%# Bind("NomeCadeira") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                            <InsertItemTemplate>
                                                <asp:TextBox ID="NomeCadeira" runat="server" Text='<%# Bind("NomeCadeira") %>'></asp:TextBox>
                                            </InsertItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("NomeCadeira") %>'></asp:Label>
                                            </ItemTemplate>
                                            <HeaderStyle Font-Bold="True" HorizontalAlign="Right" />
                                            <ItemStyle HorizontalAlign="Left" />
                                        </asp:TemplateField>

I tryed to use the following code in the DetailsView ModeChanged event, in the Page_Load event and in the Page_LoadComplete event:
Code:

        If DetailsView1.CurrentMode = DetailsViewMode.Insert Or DetailsView1.CurrentMode = DetailsViewMode.Edit Then

            Dim textBoxNomeCadeira = CType(FindControl("NomeCadeira"), TextBox)
            textBoxNomeCadeira.Focus()

        End If

but the textBoxNomeCadeira is always set to Nothing, meaning that the FindControl() function doesn't find the textBox "NomeCadeira".
How should I reference the textBox in the detailsView, in Insert or Edit Mode, to set the focus to it?

Thanks

Arcy December 16th, 2010 11:47 AM

Eventually I found that using the DataBound event of the DetailsView solved the problem.
Probably in the other events used (ModeChanged, Page_Load and Page_LoadComplete) the controls are still not available for the FindControl() function. (??!!)


All times are GMT -4. The time now is 07:52 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.