調査ページを作成しています。このページはデータベースから取得して、タイプに基づいて質問を表示します。タイプごとにユーザー コントロールを作成しました。でPage_Load
、ユーザー コントロールを次のようなプレースホルダーに配置します:- (QNO は、質問の順序を開始するために、前のページで 0 に設定したセッションです)
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);
}
}
}
ここで、「Open Ended」タイプの場合、にテキストボックスが表示さusercontrol
れます。このテキストボックスにアクセスして、その中のテキストを取得し、ボタンを押すだけで別のテキストボックスに配置したいので、静的テキストボックスを作成しましたページとそれを呼び出しましたViewTextBox
。これは私が試したものです:-
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."
}
何か案は?ページのコントロールを調べて、ユーザーコントロールのテキストボックスを見つけました:-
Response.Write(Controls[0].Controls[3].Controls[11].Controls[5].Controls[0].Controls[0].ID);
IDは、探しているテキストボックスとして表示されます。ユーザーコントロールのテキストボックスは「OpenEndedTextBox」と呼ばれます