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 Page_Load, 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)
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);
}
}
}
Now, lets say for the type “Open Ended”, it displays a textbox in the usercontrol, 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 ViewTextBox.
This is what i tried:-
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."
}
Any Ideas? I dug my way through the controls on the page to find the textbox in the usercontrol :-
Response.Write(Controls[0].Controls[3].Controls[11].Controls[5].Controls[0].Controls[0].ID);
And the ID comes up as the textbox im looking for. The textbox in the usercontrol is called “OpenEndedTextBox”
Instead of digging like that, I would suggest creating a
Method or Property in your user controlto get that text from your User Control like thisand call this Method in your button’s click event in your page containing that user control like this