在动态创建的UserControl中从文本框中检索文本

时间:2012-12-23 06:16:00

标签: c# asp.net sql-server-2012

我创建了一个调查页面,此页面从数据库中提取,根据其类型显示问题。对于我创建用户控件的每种类型。在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."
            }

任何想法?我挖掘了页面上的控件以找到usercontrol中的文本框: -

Response.Write(Controls[0].Controls[3].Controls[11].Controls[5].Controls[0].Controls[0].ID);

ID出现在我正在寻找的文本框中。 usercontrol中的文本框称为“OpenEndedTextBox”

2 个答案:

答案 0 :(得分:0)

我建议您不要像那样挖掘,而是建议创建一个Method or Property in your user control来从您的​​用户控件中获取该文本

public string GetAnsweredText()
{
   return this.AnswerTextBox.Text;
}

并在包含该用户控件的页面中按钮的单击事件中调用此方法

protected void Button1_Click(object sender, EventArgs e)
{
   UC_SurveyControl control = this.Controls[0] as UC_SurveyControl;
   if (control != null)
       string answer = control.GetAnsweredText();
}

答案 1 :(得分:0)

我建议你在这里使用递归它可能不是最好的性能解决方案,但它的工作完美。

这应该在Page_PreRender()事件中使用。

 public static T FindControlRecursive<T>(Control sourceControl, string targetControlId) 
  where T : class    
{    
   if (sourceControl == null)    
     throw new ArgumentNullException("sourceControl");    
   if (targetControlId == null)    
     throw new ArgumentNullException("targetControlId");    
   if (sourceControl.ID == targetControlId) return sourceControl as T;    
   foreach (Control control in sourceControl.Controls)    
   {    
     T controlToReturn = FindControlRecursive<T>(control, targetControlId);    
     if (controlToReturn != null) return controlToReturn as T;    
   }    
   return null;    
}  

Si vis pacem DotNetnum。