title: Gridview in user control page (ascx) is null
body: <p>I have a page from where a user can search for items and so on. I use a .ascx page to display the result in a grdview.</p>
  
  <p>The aspx page only has a seacrh box and a button and on button click I want to pass the value from inside the textbox to the ascx page and have the gridview populate with the result. But after debugging I found that the gridview is set to null after the button click. All other values are there and the result comes back from the database.</p>
  
  <p>Can some one please help. I have been looking for a solution for last few hours and have treid a few diffeerent things but still not getting anywhere.</p>
  
  <p>thanks in advance</p>
  
  <p>EDIT</p>
  
  <p>Here is my code</p>
  
  <p>This is the aspx page.</p>
  
  <pre><code>&lt;%@ Register Src="Search.ascx" TagName="Search" TagPrefix="uc1" %&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div class="content"&gt;
              &lt;div style="padding:5px;"&gt;
                  &lt;b&gt;Search&lt;/b&gt;: &lt;asp:TextBox runat="server" ID="txtSearch" /&gt;
                  &lt;asp:RequiredFieldValidator ErrorMessage="*" ValidationGroup="SearchGroup" ControlToValidate="txtSearch" ID="RequiredFieldValidator1"
                      runat="server" /&gt;
              &lt;/div&gt;
              &lt;div style="padding:5px;"&gt;
                  &lt;asp:Button Text="Search" runat="server" ValidationGroup="SearchGroup" OnClick="btnSearch_Click" ID="btnSearch" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;asp:ScriptManager runat="server" /&gt;
          &lt;asp:UpdatePanel runat="server"&gt;
              &lt;ContentTemplate&gt;
                  &lt;div class="Results"&gt;
                      &lt;uc1:Search ID="UserControl1" runat="server" /&gt;
                  &lt;/div&gt;
              &lt;/ContentTemplate&gt;
          &lt;/asp:UpdatePanel&gt;
  
      &lt;/form&gt;
  </code></pre>
  
  <p>In the ASCX page I have the following</p>
  
  <p>
      
          
      
  </p>
  
  <p>The code behind</p>
  
  <p>Dim test As Object = txtSearch.Text</p>
  
  <pre><code>Dim StockList As cls_Stock_Col = MyLOTSDB.LoadStock_Col()
  Try
      Dim dt As DataTable = New DataTable()
      Dim dcol As New DataColumn("TradeName", GetType(System.String))
      dt.Columns.Add(dcol)
  
      'Create an ID column for adding to the Datatable
      dcol = New DataColumn("PackSize", GetType(System.String))
      dt.Columns.Add(dcol)
  
      dcol = New DataColumn("PLU", GetType(System.String))
      dt.Columns.Add(dcol)
  
      dcol = New DataColumn("SOH", GetType(System.String))
      dt.Columns.Add(dcol)
  
      dcol = New DataColumn("RetailAsCurrency", GetType(System.String))
      dt.Columns.Add(dcol)
  
      For Each col As DataColumn In dt.Columns
          'Declare the bound field and allocate memory for the bound field.
          Dim bfield As New BoundField()
          'Initalize the DataField value.
          bfield.DataField = col.ColumnName
          'Initialize the HeaderText field value.
  
          Dim DisplayName As String = ""
          Dim DataFormat As String = ""
          Dim FieldType As Type = col.GetType()
          Dim StringLength As Int32
  
          GetFieldDetails(TheType, col.ColumnName, DisplayName, DataFormat, FieldType, StringLength)
          bfield.HeaderText = DisplayName
  
          If (DataFormat = "$0.00") Then
              bfield.DataFormatString = "{0:C2}"
          End If
  
  
          bfield.SortExpression = "col.ColumnName"
          'Add the newly created bound field to the GridView.
          GridView1.Columns.Add(bfield)
      Next
  
      GridView1.DataSource = StockList
      GridView1.DataBind()
  Catch ex As Exception
  
  End Try
  </code></pre>
  
  <p>I can debug and the error is thrown when I try to add the column to the gridview,</p>
  
  <p>Thanks again</p>