title: Retrieving Text from Textbox in dynamically Created UserControl
body: <p>Im creating a survey page, this page pulls from the database to display questions based on their type, For each type I have created a user control. At <code>Page_Load</code>, I place the User Control in a placeholder like this:- (QNO is a session i set to 0 on the previous page, just to start the question order)</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
          {
  
              SqlConnection Connection = DatabaseConnection.GetSurveySystemConnection();
  
              string sqlquery = "SELECT Q.[ID], Q.[Question_Order], Q.[Question], QT.[Type_Desc] FROM [Questions] Q Inner join Question_Type QT On Q.Type_ID= QT.ID Where Q.[Survey_ID] =" + Session["Survey_ID"] + "Order by Question_Order";
  
              SqlCommand cmd = new SqlCommand(sqlquery, Connection);
              cmd.CommandType = CommandType.Text;
              SqlDataAdapter da = new SqlDataAdapter();
              da.SelectCommand = cmd;
              DataTable DT = new DataTable();
              da.Fill(DT);
  
              if (DT != null)
              {
                  Session["Count"] = DT.Rows.Count;
                  QuestionLabel.Text = String.Format("{0}.{1}", DT.Rows[Convert.ToInt32(Session["QNO"])]["Question_Order"].ToString(), DT.Rows[Convert.ToInt32(Session["QNO"])]["Question"].ToString());
                  Session["Question_ID"] = DT.Rows[Convert.ToInt32(Session["QNO"])]["ID"].ToString();
  
                  if (DT.Rows[Convert.ToInt32(Session["QNO"])]["Type_Desc"].ToString() == "Multiple Choice")
                  {
                      Control uc = Page.LoadControl("UserControls/MultipleChoice.ascx");
                      PlaceHolder2.Controls.Add(uc);
                  }
                  else if (DT.Rows[Convert.ToInt32(Session["QNO"])]["Type_Desc"].ToString() == "Single Choice")
                  {
                      Control uc = Page.LoadControl("UserControls/SingleChoice.ascx");
                      PlaceHolder2.Controls.Add(uc);
                  }
                  else if (DT.Rows[Convert.ToInt32(Session["QNO"])]["Type_Desc"].ToString() == "Yes/No")
                  {
                      Control uc = Page.LoadControl("UserControls/YesOrNo.ascx");
                      PlaceHolder2.Controls.Add(uc);
                  }
                  else if (DT.Rows[Convert.ToInt32(Session["QNO"])]["Type_Desc"].ToString() == "Agree/Disagree")
                  {
                      Control uc = Page.LoadControl("UserControls/AgreeDisagree");
                      PlaceHolder2.Controls.Add(uc);
                  }
                  else if (DT.Rows[Convert.ToInt32(Session["QNO"])]["Type_Desc"].ToString() == "Rating")
                  {
                      Control uc = Page.LoadControl("UserControls/Rating.ascx");
                      PlaceHolder2.Controls.Add(uc);
                  }
                  else if (DT.Rows[Convert.ToInt32(Session["QNO"])]["Type_Desc"].ToString() == "Open Ended")
                  {
                      Control uc = Page.LoadControl("UserControls/OpenEnded.ascx");
                      PlaceHolder2.Controls.Add(uc);
                  }
              }
          }
  </code></pre>
  
  <p>Now, lets say for the type "Open Ended", it displays a textbox in the <code>usercontrol</code>, I want to access this textbox and retrieve the text inside it and put it in another textbox at the push of a button, I created a static textbox on the page and called it <code>ViewTextBox</code>.
  This is what i tried:- </p>
  
  <pre><code> protected void Button1_Click(object sender, EventArgs e)
              {
      TextBox t = Controls[0].Controls[3].Controls[11].Controls[5].Controls[0].Controls[0] as TextBox;
      ViewTextBox.Text = t.Text; //"Object reference not set to an instance of an object."
              }
  </code></pre>
  
  <p>Any Ideas? I dug my way through the controls on the page to find the textbox in the usercontrol :-</p>
  
  <pre><code>Response.Write(Controls[0].Controls[3].Controls[11].Controls[5].Controls[0].Controls[0].ID);
  </code></pre>
  
  <p>And the ID comes up as the textbox im looking for. The textbox in the usercontrol is called "OpenEndedTextBox"</p>