0

The formview has more than 1 Panels. My textbox is in the first panel. If I use this

TextBox myTxtBox = (TextBox)myformView.Row.FindControl("pnlID").FindControl("mytextbox"); <- does not work

Panel mypanel = (Panel)myformView.Row.FindControl("pnlID"); <- this works
TextBox myTxtBox = (TextBox) FindControlRecursive(mypanel,'mytextbox'); <-- this does not work

Can someone help? As as side question, I used a function FindControlIterative but I do not know which references to include for LinkedList

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136

2 Answers2

0

The following works for me:

Markup

<asp:FormView ID="formView1" runat="server">
    <ItemTemplate>
        <asp:Panel ID="pnlID" runat="server">
            <asp:TextBox ID="mytextbox" runat="server"></asp:TextBox>
        </asp:Panel>
    </ItemTemplate>
</asp:FormView>

Code behind

TextBox myTxtBox = (TextBox)FindControlRecursive(formView1,"mytextbox");
jdavies
  • 12,784
  • 3
  • 33
  • 33
  • First of all I get errors for `LinkedList ctls = new LinkedList();` inside the FindControlIterative because I do know what is the required namespaces. – TheTechGuy Oct 06 '11 at 18:48
  • The namespace is `System.Collections.Generic`. See: [LinkedList](http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx) – jdavies Oct 06 '11 at 18:50
  • Oops I do use master page as well. It asks for the variable T if I include Generic. – TheTechGuy Oct 06 '11 at 18:50
  • The solution provided by user "stelianx" in the forum post uses `LinkedList`. TBH there is not going to be a measurable different between the FindControlRecursive and FindControlIterative methods. – jdavies Oct 06 '11 at 18:52
  • At what point in the page life cycle are you executing the code to retrieve the TextBox? – jdavies Oct 06 '11 at 21:08
  • In my test project, I am putting the code in Click event of a button. I go into debug mode and it returns Null. Thanks – TheTechGuy Oct 06 '11 at 21:32
0

My answer: @jdavies solution is right. I was passing the wrong control to the function. I realized my formView1 was actually inside another panel, thus formview was not directly visible to code behind.

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136