title: Find nested GridView with Button Click?
body: <p>I would like to find the nested GridView <code>gvR</code> on a button click, so that I can add a row to it.  I will post the GridView markup and then the code for adding the row to the selected nested grid.</p>
  
  <pre><code>&lt;asp:LinkButton ID="btnAdd" runat="server" Text="Add Room" 
              onclick="btnAdd_Click"&gt;&lt;/asp:LinkButton&gt;
          &lt;asp:GridView ID="gvRP" runat="server" AutoGenerateColumns="false" 
              onrowdatabound="gvRP_RowDataBound" 
              onrowediting="gvRP_RowEditing"&gt;
          &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Room" ItemStyle-Width="100%"&gt;
          &lt;ItemTemplate&gt;
          &lt;asp:Label runat="server" Text="Room"&gt;&lt;/asp:Label&gt;
          &lt;asp:DropDownList ID="ddlRoom" runat="server" AutoPostBack="True" DataTextField="Name"
              DataValueField="Id" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlRoom_SelectedIndexChanged"&gt;
              &lt;asp:ListItem Value="-1"&gt;Select...&lt;/asp:ListItem&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:Label runat="server" AssociatedControlID="ddlRate" Text="Rate" ID="lblRate"&gt;&lt;/asp:Label&gt;&lt;asp:DropDownList
              ID="ddlRate" runat="server" AppendDataBoundItems="true" DataTextField="Name"
              DataValueField="Id"&gt;
              &lt;asp:ListItem Value="-1"&gt;Select...&lt;/asp:ListItem&gt;
          &lt;/asp:DropDownList&gt;
  
          &lt;asp:Label  runat="server" Text="Adults"&gt;&lt;/asp:Label&gt;
          &lt;asp:TextBox ID="txtAdults" Text='&lt;%#Bind("Adults") %&gt;' runat="server" Width="25px"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:Label  runat="server" Text="Children"&gt;&lt;/asp:Label&gt;
          &lt;asp:TextBox ID="txtChildren" Text='&lt;%#Bind("Children") %&gt;' runat="server"  Width="25px"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:Label runat="server" Text="Check In"&gt;&lt;/asp:Label&gt;
          &lt;asp:TextBox ID="txtCheckIn" Text='&lt;%#Bind("CheckIn") %&gt;' runat="server" Width="75px"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:Label  runat="server" Text="Check Out"&gt;&lt;/asp:Label&gt;
          &lt;asp:TextBox ID="txtCheckOut" Text='&lt;%#Bind("CheckOut") %&gt;' runat="server"  Width="75px"&gt;&lt;/asp:TextBox&gt;
  
          &lt;h3&gt;Rates&lt;/h3&gt;
          &lt;asp:GridView ID="gvR" runat="server" AutoGenerateColumns="false"&gt;
          &lt;Columns&gt;
          &lt;asp:BoundField DataField="Name" HeaderText="Rate" /&gt;
          &lt;asp:BoundField DataField="Effective" HeaderText="Effective" /&gt;
          &lt;asp:BoundField DataField="Expire" HeaderText="Expire" /&gt;
          &lt;asp:BoundField DataField="Amount" HeaderText="Amount" /&gt;
          &lt;asp:BoundField DataField="Code" HeaderText="Currency" /&gt;
          &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
          &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Code for adding Rate:</p>
  
  <pre><code>protected void AddRate(object sender, EventArgs e)
  {
      lstRateDetails = (List&lt;RateDetail&gt;)ViewState["Rates"];
  
      lstRateDetails.Insert(0, new RateDetail());
      //Have no idea what to do here? I have also tried gvRatePlans.TemplateControl...
      GridView gvR = (GridView)gvRP.FindControl("gvR");
      gvR.DataSource = lstRateDetails;
      gvR.DataBind();
  
      ViewState["Rates"] = lstRateDetails;
  }
  </code></pre>