title: Controls null in Page_Load and Page_PreRenderComplete
body: <p>This problem has been bothering me for weeks now. This page is basically a template and i want to change the text property of the various labels to whatever info i get from my database. This also needs to be done before the user sees the page. All the labels are null in the page_load event so i read online that i should wait later in the page life cycle. When i put it in the page_prerendercomplete event it works unreliably.Sometimes it works locally sometimes it doesnt. It never works live though. 
  I also tried to use the FindControl method but that also fails.When i created a new test page, it did seem to work in the prerender complete event, but by the time i add all my code in it fails again.</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Library.aspx.cs" Inherits="Library" %&gt;
  
      &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"&gt;
      &lt;/asp:Content&gt;
      &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"&gt;
          &lt;div id = "ContentContainer" align="center"&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;h1 runat = 'server' id = 'HeaderTitle' align="center" 
  
                  style="margin: 20px 20px 20px 20px; color: #000000; width: 750px; border-bottom-style: dotted; border-bottom-width: 1px; border-bottom-color: #808080;"&gt;
              &lt;asp:Literal ID="Literal1" runat="server"&gt;New C# All&lt;/asp:Literal&gt;&lt;/h1&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
  
              &lt;div id = "Main" align="left"&gt;
              Language:&lt;asp:DropDownList ID="DropDownList1" runat="server" 
                  AutoPostBack="True" DataSourceID="LinqDataSource1" DataTextField="Language1" 
                  DataValueField="Language1"&gt;
              &lt;/asp:DropDownList&gt;
              Types:&lt;asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"&gt;
                      &lt;asp:ListItem Selected="True"&gt;All&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Snippets&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Tutorials&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Collections&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
              Sort By:&lt;asp:DropDownList ID="DropDownList5" runat="server" AutoPostBack="True"&gt;
                      &lt;asp:ListItem&gt;New&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Popular&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Your&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;asp:LinqDataSource ID="LinqDataSource1" runat="server" 
                  ContextTypeName="DataClassesDataContext" EntityTypeName="" 
                  TableName="Languages"&gt;
              &lt;/asp:LinqDataSource&gt;
              &lt;asp:Panel ID="Panel1" runat="server"&gt;
              &lt;asp:Label ID="Label1" runat="server" Text="There are no records to display"&gt;&lt;/asp:Label&gt;
              &lt;/asp:Panel&gt;
              &lt;/div&gt;
              &lt;div id = "Side" align="right"&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/asp:Content&gt;
      &lt;asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"&gt;
          &lt;div id = "ContentContainer" align="center"&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;h1 runat = 'server' id = 'HeaderTitleLoggedin' align="center" 
  
                  style="margin: 20px 20px 20px 20px; color: #000000; width: 750px; border-bottom-style: dotted; border-bottom-width: 1px; border-bottom-color: #808080;"&gt;
              &lt;asp:Literal ID="Literal2" runat="server"&gt;New C# All&lt;/asp:Literal&gt;&lt;/h1&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
  
              &lt;div id = "Main" align="left"&gt;
              Language:&lt;asp:DropDownList ID="DropDownList3" runat="server" 
                  AutoPostBack="True" DataSourceID="LinqDataSource2" DataTextField="Language1" 
                  DataValueField="Language1"&gt;
                      &lt;asp:ListItem Selected="True"&gt;All&lt;/asp:ListItem&gt;
              &lt;/asp:DropDownList&gt;
              Types:&lt;asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True"&gt;
                      &lt;asp:ListItem Selected="True"&gt;All&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Snippet&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Tutorial&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Collection&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
              Sort By:&lt;asp:DropDownList ID="DropDownList6" runat="server" AutoPostBack="True"&gt;
                      &lt;asp:ListItem&gt;New&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Popular&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Your&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;asp:LinqDataSource ID="LinqDataSource2" runat="server" 
                  ContextTypeName="DataClassesDataContext" EntityTypeName="" 
                  TableName="Languages"&gt;
              &lt;/asp:LinqDataSource&gt;
              &lt;asp:Panel ID="Panel2" runat="server"&gt;
                  &lt;asp:Label ID="Label2" runat="server" Text="There are no records to display"&gt;&lt;/asp:Label&gt;
  
  
              &lt;/asp:Panel&gt;
              &lt;/div&gt;
              &lt;div id = "Side" align="right"&gt;
                  &lt;div id = "Login"&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Heres the code behind file</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Text;
  using System.Collections;
  
  public partial class Library : BasePage//System.Web.UI.Page
  {
      string language;
      string author;
      string title;
      List&lt;string&gt; compare = new List&lt;string&gt;();
      string compareto = "";
      protected void Page_Load(object sender, EventArgs e)
      {
      }
      protected void Page_PreRenderComplete(object sender, EventArgs e)
      {
          language = Request.QueryString["language"];
          author = Request.QueryString["author"];
          title = Request.QueryString["title"];
          if (string.IsNullOrEmpty(language + author + title))
          {
              FillPage();
          }
          else
          {
              FillBy();
          }
          //FillPage();
      }
      public static ArrayList GetContentByType(string ContentType, string language)
      {
  
          ArrayList list = new ArrayList();
  
          DataClassesDataContext db = new DataClassesDataContext();
  
          if (ContentType == "All" &amp; language == "All")
          {
              var allcontents = db.User_Contents;
              var allcontent = from ac in db.User_Contents
                               where ac.isApproved.ToString().Contains(char.Parse("t").ToString())
                               select ac;
              foreach (User_Content ac in allcontent)
              {
                  list.Add(ac);
              }
  
  
          }
          if (ContentType != "All" &amp; language != "All")
          {
  
              var contents = db.User_Contents;
              var content = from c in db.User_Contents
                            where c.Type == ContentType
                            where c.Languages == language
                            where c.isApproved.ToString().Contains(char.Parse("t").ToString())
                            select c;
              foreach (User_Content c in content)
              {
                  list.Add(c);
              }
          }
          if (ContentType != "All" &amp; language == "All")
          {
  
              var contents = db.User_Contents;
              var content = from c in db.User_Contents
                            where c.Type == ContentType
                            where c.isApproved.ToString().Contains(char.Parse("t").ToString())
                            select c;
              foreach (User_Content c in content)
              {
                  list.Add(c);
              }
          }
          if (ContentType == "All" &amp; language != "All")
          {
  
              var contents = db.User_Contents;
              var content = from c in db.User_Contents
                            where c.Languages == language
                            where c.isApproved.ToString().Contains(char.Parse("t").ToString())
                            select c;
              foreach (User_Content c in content)
              {
                  list.Add(c);
              }
          }
  
          return list;
      }
      private void FillPage()
      {
  
          try
          {
              try
              {
                  Literal1.Text = DropDownList5.SelectedItem.Text + " " + DropDownList1.SelectedItem.Text + " " + DropDownList2.SelectedItem.Text;
                  Label1.Text = "";
              }
              catch
              {
                  (FindControl("Literal1") as Literal).Text = DropDownList5.SelectedItem.Text + " " + DropDownList1.SelectedItem.Text + " " + DropDownList2.SelectedItem.Text;
                  (FindControl("Label1") as Label).Text = "";
              }
          }
          catch
          {
              try
              {
                  Literal2.Text = DropDownList6.SelectedItem.Text + " " + DropDownList3.SelectedItem.Text + " " + DropDownList4.SelectedItem.Text;
                  Label2.Text = "";
              }
              catch
              {
                  (FindControl("Literal2") as Literal).Text = DropDownList6.SelectedItem.Text + " " + DropDownList3.SelectedItem.Text + " " + DropDownList4.SelectedItem.Text;
                  (FindControl("Label2") as Label).Text = "";
              }
          }
          DataClassesDataContext db = new DataClassesDataContext();
  
          ArrayList ContentList = new ArrayList();
          if (!IsPostBack)
          {
              try
              {
                  foreach (User_Content con in GetContentByType("All", DropDownList1.SelectedItem.Text))
                  {
                      ContentList.Add(con);
                  }
              }
              catch
              {
                  foreach (User_Content con in GetContentByType("All", DropDownList3.SelectedItem.Text))
                  {
                      ContentList.Add(con);
                  }
  
              }
          }
          else
          {
              try
              {
                  foreach (User_Content con in GetContentByType(DropDownList2.SelectedItem.Text,DropDownList1.SelectedItem.Text))
                  {
                      ContentList.Add(con);
                  }
              }
              catch
              {
                  foreach (User_Content con in GetContentByType(DropDownList4.SelectedItem.Text,DropDownList3.SelectedItem.Text))
                  {
                      ContentList.Add(con);
                  }
              }
          }
          StringBuilder sb = new StringBuilder();
  //            &lt;tr&gt;
  //                &lt;th&gt;Author: &lt;/th&gt;
  //                &lt;td&gt;{1}&lt;/td&gt;
  //            &lt;/tr&gt;
  
          foreach (User_Content content in ContentList)
          {
              string description = "";
              string useridimg = "";
              var images = db.User_Infos;
              var image = from i in db.User_Infos
                          where i.UserID == content.UserID
                          select i.ProfilePicLink;
              useridimg = image.First&lt;string&gt;();
              if ((content.ContentDescription).Length &gt; 100)
              {
                  content.ContentDescription.Remove(100);
              }
              else
              {
                  description = content.ContentDescription;
              }
              string unit = "Days";
              int time = 0;
              var difference = ((DateTime.Now).Subtract(content.ContentCreationDate.Value));
              if ((difference).TotalHours &lt; 1)
              {
                  time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalSeconds;
                  unit = "seconds";
              }
              if ((difference).TotalHours &lt; 24)
              {
                  time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalHours;
                  unit = "hours";
              }
              if ((difference).TotalHours &gt; 24)
              {
                  time = ((int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalDays);
                  unit = "days";
              }
              if ((difference).TotalDays &gt; 365)
              {
                  unit = "over a year ago";
              }
              string longago = "";
  
              if (time != 0)
              {
                  longago = time +" "+ unit + " ago";
              }
              else
              {
                  longago = unit;
  
              }
              sb.Append(
                  string.Format(
                  @"&lt;a href = 'ContentDisplay.aspx?ContentID={8}'&gt; &lt;table class = 'contentTable'&gt;
              &lt;tr&gt;
                  &lt;th rowspan='6' width='150px'&gt;&lt;img width='128' height='128' runat='server' src='{7}' /&gt;&lt;/th&gt;
                  &lt;th width = '50px'&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;h3&gt;{0}&lt;/h3&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;td colspan='2'&gt; Posted {5} by {1} &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;th&gt;Language: &lt;/th&gt;
                  &lt;td&gt;{6}&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;th&gt;Description: &lt;/th&gt;
                  &lt;td&gt;{2}&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;th&gt;Rating: &lt;/th&gt;
                  &lt;td&gt;{3}&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;th&gt;Tags: &lt;/th&gt;
                  &lt;td&gt;{4}&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;/table&gt;&lt;/a&gt;",
                      content.ContentName, content.ContentAuthor, description, content.ContentRating, content.ContentTags, longago, content.Languages, useridimg, content.ContentID));
              try
              {
  
                  Label1.Text = sb.ToString();
              }
              catch
              {
                  Label2.Text = sb.ToString();
              }
          }
      }
      public void FillFromFile()
      {
  
      }
      public void FillBy()
      {
          DataClassesDataContext db = new DataClassesDataContext();
          var ContentByLanguage = from l in db.User_Contents
                                  //where l.Languages == language
                                  select l;
          var ContentByAuthor = from a in db.User_Contents
                                //where a.ContentAuthor == author
                                select a;
          var ContentByTitle = from t in db.User_Contents
                               //where t.ContentName == title
                               select t;
          #region All Cases
          if (!string.IsNullOrEmpty(language) &amp; string.IsNullOrEmpty(author) &amp; string.IsNullOrEmpty(title))
          {
              var allcontent = from c in db.User_Contents
                        select c.Languages;
              compare = allcontent.ToList();
          }
          if (!string.IsNullOrEmpty(author) &amp; string.IsNullOrEmpty(language) &amp; string.IsNullOrEmpty(title))
          {
              var allauthors = from a in db.User_Contents
                               select a.ContentAuthor;
              compare = allauthors.ToList();
          }
          if (!string.IsNullOrEmpty(title) &amp; string.IsNullOrEmpty(author) &amp; string.IsNullOrEmpty(language))
          {
              var alltitles = from t in db.User_Contents
                              select t.ContentName;
              compare = alltitles.ToList();
          }
          if (!string.IsNullOrEmpty(language) &amp; !string.IsNullOrEmpty(author) &amp; string.IsNullOrEmpty(title))
          {
              //var allLanguagesandAuthors = from la in db.User_Contents
              //                             where la.Languages == language
              //                             where la.ContentAuthor == author
              //                             select la.
          }
          #endregion
          foreach (var possibility in OrderByProximity(compareto, compare))
          {
              var content = from c in db.User_Contents
                            where c.Languages == possibility.Key
                            select c;
  
  
  
              StringBuilder sb = new StringBuilder();
              //string description = "";
              //string useridimg = "";
              //var images = db.User_Infos;
              //var image = from i in db.User_Infos
              //            where i.UserID == content.UserID
              //            select i.ProfilePicLink;
              //useridimg = image.First&lt;string&gt;();
              //if ((content.ContentDescription).Length &gt; 100)
              //{
              //    content.ContentDescription.Remove(100);
              //}
              //else
              //{
              //    description = content.ContentDescription;
              //}
              //string unit = "Days";
              //int time = 0;
              //var difference = ((DateTime.Now).Subtract(content.ContentCreationDate.Value));
              //if ((difference).TotalHours &lt; 1)
              //{
              //    time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalSeconds;
              //    unit = "seconds";
              //}
              //if ((difference).TotalHours &lt; 24)
              //{
              //    time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalHours;
              //    unit = "hours";
              //}
              //if ((difference).TotalHours &gt; 24)
              //{
              //    time = ((int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalDays);
              //    unit = "days";
              //}
              //if ((difference).TotalDays &gt; 365)
              //{
              //    unit = "over a year ago";
              //}
              //string longago = "";
  
              //if (time != 0)
              //{
              //    longago = time +" "+ unit + " ago";
              //}
              //else
              //{
              //    longago = unit;
  
              //}
              sb.Append(
                  string.Format(
                  @"&lt;a href = 'ContentDisplay.aspx?ContentID={7}'&gt; &lt;table class = 'contentTable'&gt;
              &lt;tr&gt;
                  &lt;th rowspan='6' width='150px'&gt;&lt;img width='128' height='128' runat='server' src='{6}' /&gt;&lt;/th&gt;
                  &lt;th width = '50px'&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;h3&gt;{0}&lt;/h3&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;td colspan='2'&gt; Posted by {1} &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;th&gt;Language: &lt;/th&gt;
                  &lt;td&gt;{5}&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;th&gt;Rating: &lt;/th&gt;
                  &lt;td&gt;{2}&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;th&gt;Tags: &lt;/th&gt;
                  &lt;td&gt;{3}&lt;/td&gt;
              &lt;/tr&gt;
  
             &lt;/table&gt;&lt;/a&gt;",
                      content.First().ContentName, content.First().ContentAuthor, content.First().ContentRating, content.First().ContentTags, content.First().Languages, content.First().ContentID));
              try
              {
  
                  Label1.Text = sb.ToString();
              }
              catch
              {
                  Label2.Text = sb.ToString();
              }
          }
  
  
              }
  
      public List&lt;KeyValuePair&lt;string, int&gt;&gt; OrderByProximity(string input, List&lt;string&gt; possibilities)
      {
          List&lt;KeyValuePair&lt;string, int&gt;&gt; RawData = new List&lt;KeyValuePair&lt;string,int&gt;&gt;();
          foreach (var i in possibilities)
          {
              RawData.Add(new KeyValuePair&lt;string, int&gt;(i, FindClosestMatch(i, input)));
          }
          RawData.Sort(Compare1);
          return RawData;
      }
      static int Compare1(KeyValuePair&lt;string, int&gt; a, KeyValuePair&lt;string, int&gt; b)
      {
          return a.Key.CompareTo(b.Key);
      }
  
      static int Compare2(KeyValuePair&lt;string, int&gt; a, KeyValuePair&lt;string, int&gt; b)
      {
          return a.Value.CompareTo(b.Value);
      }
  
      protected void Reload_Click(object sender, EventArgs e)
      {
          FillPage();
      }
      public int FindClosestMatch(string s, string t)
      //{
      //public static int Compute(string s, string t)
      {
          int n = s.Length;
          int m = t.Length;
          int[,] d = new int[n + 1, m + 1];
  
          // Step 1
          if (n == 0)
          {
              return m;
          }
  
          if (m == 0)
          {
              return n;
          }
  
          // Step 2
          for (int i = 0; i &lt;= n; d[i, 0] = i++)
          {
          }
  
          for (int j = 0; j &lt;= m; d[0, j] = j++)
          {
          }
  
          // Step 3
          for (int i = 1; i &lt;= n; i++)
          {
              //Step 4
              for (int j = 1; j &lt;= m; j++)
              {
                  // Step 5
                  int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
  
                  // Step 6
                  d[i, j] = Math.Min(
                      Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                      d[i - 1, j - 1] + cost);
              }
          }
          // Step 7
          return d[n, m];
      }
  
  }
  </code></pre>