title: Using FindControl to find a DropDownList within a Gridview EditItemTemplate
body: <p>When I try to add Items to my new instance of the DropDownList I get an "Object reference not set to an instance of an object" on the editPriorityDropDown. My best guess is the FindControl is failing to find the DropDownList. I think I have somehow escaped the scope of the FindControl's reach, yet I do the exact same thing for another DropDown, but in the Page_Load, which works fine.</p>
  
  <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not Page.IsPostBack Then
  
      End If
  
      Dim PriorityDropDown As DropDownList = TryCast(gvProduct.HeaderRow.FindControl("ddlOrderPriorityHeader"), DropDownList)
  
      Dim index As Integer = 1
      While index &lt;= gvProduct.Rows.Count
          Dim newItem As New ListItem
          newItem.Value = index
          newItem.Text = index.ToString
  
          PriorityDropDown.Items.Add(newItem)
  
          index = index + 1
      End While
  
      '' check login seesion
      'Functions.authentication_check()
  
  End Sub
  </code></pre>
  
  <p>But when I try to do the same thing later in a different subroutine it errors. </p>
  
  <pre><code>Protected Sub PopulateOrderPriority()
      If ViewState("SelectedRowIndex") &gt; -1 Then
          'Response.Write(" Index: " &amp; ViewState("SelectedRowIndex"))
          Dim editPriorityDropDown As DropDownList = TryCast(Me.gvProduct.Rows(ViewState("SelectedRowIndex")).FindControl("ddlOrderPriorityEdit"), DropDownList)
  
          Dim index As Integer = 1
          While index &lt;= gvProduct.Rows.Count
              Dim newItem As New ListItem
              newItem.Value = index
              newItem.Text = index.ToString
  
              editPriorityDropDown.Items.Add(index)
  
              index = index + 1
          End While
      End If
  End Sub
  </code></pre>
  
  <p>Here is just a piece of the relevent markup.</p>
  
  <pre><code>&lt;asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" DataKeyNames="Product_ID"
      DataSourceID="sdsProduct" Caption="Products" CaptionAlign="Left" EnableModelValidation="True"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField ShowHeader="False"&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:LinkButton ID="lnkbtnUpdate" runat="server" CausesValidation="True" CommandName="UpdateProd"
                      Text="Update" ValidationGroup="vgUpdateProduct"&gt;&lt;/asp:LinkButton&gt;
                  &amp;#160;&lt;asp:LinkButton ID="lnkbtnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
                      Text="Cancel"&gt;&lt;/asp:LinkButton&gt;
              &lt;/EditItemTemplate&gt;
              &lt;HeaderTemplate&gt;
                  &lt;asp:Button ID="lnkbtnInsertHeader" runat="server" CommandName="InsertHeader" Text="Insert"
                      ValidationGroup="vgHeaderProduct" /&gt;
              &lt;/HeaderTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="lnkbtnEdit" runat="server" CausesValidation="False" CommandName="Edit"
                      Text="Edit"&gt;&lt;/asp:LinkButton&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Order Priority" SortExpression="Order_Priority"&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:DropDownList ID="ddlOrderPriorityEdit" runat="server"&gt;
                  &lt;/asp:DropDownList&gt;
                  &lt;%--&lt;asp:DropDownList ID="ddlOrderPriorityEdit" runat="server" SelectedValue='&lt;%# Bind("Product_Priority") %&gt;'
                      DataSourceID="sdsProductActive" DataTextField="Product_Priority"
                      DataValueField="Product_ID"&gt;
                  &lt;/asp:DropDownList&gt;--%&gt;
              &lt;/EditItemTemplate&gt;
              &lt;HeaderTemplate&gt;
                  Order Priority&lt;br /&gt;
                  &lt;asp:DropDownList ID="ddlOrderPriorityHeader" runat="server"&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;/HeaderTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:DropDownList ID="ddlOrderPriorityRead" runat="server" Enabled="false"&gt;
                  &lt;/asp:DropDownList&gt;
                  &lt;%--&lt;asp:DropDownList ID="ddlOrderPriorityRead" runat="server" Enabled="false" SelectedValue='&lt;%# Bind("Product_Type") %&gt;'
                      DataSourceID="sdsProductActive" DataTextField="Product_Priority"
                      DataValueField="Product_ID"&gt;
                  &lt;/asp:DropDownList&gt;--%&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>Let me know what I'm doing wrong here, and thanks in advance for your answer(s)!</p>