title: Using FindControl: Accessing Controls in a Formview
body: <p>I'm developing a simple Wedding List application, where guests can reserve the present they want to buy for the bride and groom. The Reserve page wraps a few fields inside a couple of panels, all wrapped inside a FormView.</p>
  
  <p>The user enters their name, email and the quantity of items that they want to reserve, and the page will make the necessary reservations in the DB.</p>
  
  <p>My first problem was that in FormView_ItemCommand, I couldn't reference any of the other controls in the FormView.... I figured this was a case for FindControl - but why do I need to for a Formview when I've never needed it for ListViews or DetailViews?</p>
  
  <p>Secondly, I know the following code works..</p>
  
  <pre><code>Dim oCtrl as TextBox = Me.fvwReservation.FindControl("txtEmail")
  Dim Test As String = oCtrl.Text
  </code></pre>
  
  <p>...but why can't I use...</p>
  
  <pre><code>Dim Test As String = Me.fvwReservation.FindControl("txtEmail").Text
  </code></pre>
  
  <p>??</p>
  
  <p>Finally, I don't think I need it on this occasion, but I've been researching recursive FindControl variants, but I haven't actually found one that actually compiles! Any suggestions?</p>
  
  <p>It's a lot for one post - thanks in advance.</p>
  
  <p>Gratuitous Code Snippet:</p>
  
  <pre><code>&lt;asp:FormView ID="fvwReservation" runat="Server" DataSourceID="dsGift"&gt;
       &lt;ItemTemplate&gt;
        &lt;asp:Panel runat="server" ID="pnlDetails"&gt;
         &lt;h3&gt;Reserve Item: &lt;%#Eval("ShortDesc")%&gt;&lt;/h3&gt;
         &lt;p&gt;You have chosen to reserve the &lt;em&gt;&lt;%#Eval("LongDesc")%&gt;&lt;/em&gt; gift.&lt;/p&gt;
         &lt;p&gt;Please enter your details below to confirm the reservation.&lt;/p&gt;
        &lt;/asp:Panel&gt;
        &lt;asp:Panel runat="server" ID="pnlConfirm"&gt;
         &lt;div class="row"&gt;
          &lt;asp:Label runat="server" CssClass="label"&gt;Name:&lt;/asp:Label&gt;&lt;asp:TextBox ID="txtName" MaxLength="50" runat="server" CssClass="Field" /&gt;
          &lt;asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="You must specify your Name" ControlToValidate="txtName" /&gt;
         &lt;/div&gt;
         &lt;div class="row"&gt;
          &lt;asp:Label runat="server" CssClass="label"&gt;Email:&lt;/asp:Label&gt;&lt;asp:TextBox ID="txtEmail" MaxLength="100" runat="server" CssClass="Field"/&gt;
          &lt;asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="You must specify your Email Address" ControlToValidate="txtEmail" /&gt;
          &lt;asp:RegularExpressionValidator ID="regexEmail" ValidationExpression="^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$" runat="server" ErrorMessage="Please enter a valid Email Address" ControlToValidate="txtEmail" /&gt;
         &lt;/div&gt;
         &lt;div class="row"&gt;
          &lt;asp:Label runat="server" CssClass="label"&gt;Quantity (max &lt;%#Eval("QtyRemaining")%&gt;):&lt;/asp:Label&gt;&lt;asp:TextBox ID="iQty" MaxLength="2" runat="server" CssClass="Field" /&gt;
          &lt;asp:RangeValidator ID="rvQty" runat="server" ErrorMessage="The Quantity mmust be between 1 and 10" MinimumValue="1" MaximumValue="10" ControlToValidate="iQty" /&gt;
         &lt;/div&gt;
         &lt;div class="row"&gt;
          &lt;asp:Label runat="server" CssClass="label"&gt;&amp;nbsp;&lt;/asp:Label&gt;
          &lt;asp:Button ID="btnReserve" Text="Confirm Reservation" CommandName="Reserve" runat="server" /&gt;
         &lt;/div&gt;
        &lt;/asp:Panel&gt;      
       &lt;/ItemTemplate&gt;
      &lt;/asp:FormView&gt;
  </code></pre>