C# 在占位符中查找控件

C# 在占位符中查找控件,c#,asp.net,ajax,visual-studio,user-controls,C#,Asp.net,Ajax,Visual Studio,User Controls,我正在使用AJAX创建一个用户控件,其中包含一个面板,该面板根据条件包含label和RadioButtonList或CheckBoxList。 在.aspx页面中有一个占位符,该控件应该位于其中。 我需要从占位符中查找列表 我试过这个: public static int id = 1; QuestionPanelControl q1 ; protected void Page_Load(object sender, EventArgs e) { if

我正在使用AJAX创建一个用户控件,其中包含一个面板,该面板根据条件包含label和RadioButtonList或CheckBoxList。 在.aspx页面中有一个占位符,该控件应该位于其中。 我需要从占位符中查找列表 我试过这个:

 public static int id = 1;
    QuestionPanelControl q1 ;

    protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
        {
           LoadQuestionPanelControl();
       }
    }

    //Next Button
    protected void Button1_Click(object sender, EventArgs e)
    { 
        id++;
        if (id <= 10)
        {
            //LoadQuestionPanelControl();
            PlaceHolder p = (PlaceHolder)Page.FindControl("PlaceHolder1");
            QuestionPanelControl c1 = (QuestionPanelControl)p.FindControl("QuestionPanelControl1");
           // QuestionPanelControl c1 = (QuestionPanelControl)p.FindControl("Panel_Question");
            RadioButtonList rb = c1.ChildRadioButtonList;
            if (rb.SelectedIndex == 0)
            {
                //DB 
            }
            else if (rb.SelectedIndex == 1)
            {
                //DB
            }
            else
            {
                Lsb_Unanswered.Items.Add("Question #" + id);
            }

            LoadQuestionPanelControl();

        }
    }

public void LoadQuestionPanelControl()
    {
        Session.Add("ID",id);
        q1= new QuestionPanelControl();
        q1.ID = "QuestionPanelControl1";
        Control c = Page.LoadControl("QuestionPanelControl.ascx");
        PlaceHolder1.Controls.Clear();
        PlaceHolder1.Controls.Add(c);

    }
publicstaticintid=1;
问题控制q1;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
LoadQuestionPanelControl();
}
}
//下一个按钮
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{ 
id++;
如果(id请尝试以下代码:

PlaceHolder p = (PlaceHolder)FindControlRecursive(Page, "PlaceHolder1");



public static Control FindControlRecursive(Control ctrl, string controlID)
{
 if (string.Compare(ctrl.ID, controlID, true) == 0)
 {
  // We found the control!
  return ctrl;
 }
 else
 {
  // Recurse through ctrl's Controls collections
  foreach (Control child in ctrl.Controls)
  {
   Control lookFor = FindControlRecursive(child, controlID);

   if (lookFor != null)
   return lookFor;  // We found the control
  }

 // If we reach here, control was not found
 return null;
 }
}
编辑:

   public void LoadQuestionPanelControl()
    {
        Session.Add("ID",id);
        Control c = Page.LoadControl("QuestionPanelControl.ascx");
        var q1= c as QuestionPanelControl;
        if(q1 != null)
        {
            q1.ID = "QuestionPanelControl1";

            PlaceHolder1.Controls.Clear();
            PlaceHolder1.Controls.Add(q1);
        }

    }

然后查看占位符的控件.Count是否不是0。

如何以及何时将QuestionPanelControl添加到占位符?我尝试了,但在将radioButtonList添加到控件内的面板时,它引发了一个异常。对象引用未设置为对象异常的实例。您可以发布部分出现此异常的用户控制代码?ListItem rb_Answer_True=新建ListItem(“True”,“T”);ListItem rb_Answer_False=新建ListItem(“False”,“F”);//将列表项添加到RadioButton列表rbl_Answers.items.Add(rb_Answer_True);rbl_Answers.items.Add(rb_Answer_False);//添加RadioButtonList tp panel_Question.Controls.Add(rbl_Answers);//很抱歉,我无法格式化它,但我正在尝试将两个listItems添加到RadioButtoList,然后将其添加到面板。我之前已经尝试过了,但我不知道如何调用它。我应该编写什么来代替控件ctrl-prameter?@noor:您应该编写页面,还是这意味着此函数将位于.ascx页面中?@noor:此函数是静态的,它可以放在任何地方