在占位符中查找控件

3

我正在使用 AJAX 制作一个用户控件,其中包含一个面板,该面板根据条件包含标签和 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);

    }

当我使用断点时,我发现p的Controls属性为0。
注意:ChildRadioButtonList是QuestionPanelControl中的一个属性。 有什么建议吗...

你是如何以及何时将QuestionPanelControl添加到占位符中的? - Bala R
2个回答

3

试试这段代码:

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;
 }
}

我之前已经尝试过了,但是我不知道该如何调用它。在 Control ctrl 参数的位置应该写什么? - noor
@noor:你应该写一个页面或者这个页面。 - vvk
你的意思是这个函数会在 .ascx 页面中吗? - noor
@noor:这个函数是静态的,可以放在任何地方。 - vvk

0

编辑:

   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);
        }

    }

并查看占位符的 Controls.Count 是否为非 0 值。


我尝试过这样做,但是在将radioButtonList添加到控件内部的面板时抛出了异常。 - noor
对象引用未设置到对象的实例 Exception。 - noor
你能否发布用户控件代码的部分,其中包含此异常发生的位置? - Bala R
ListItem rb_Answer_True = new ListItem("True", "T"); ListItem rb_Answer_False = new ListItem("False", "F"); //将列表项添加到RadioButtonList rbl_Answers.Items.Add(rb_Answer_True);
rbl_Answers.Items.Add(rb_Answer_False); //将RadioButtonList添加到面板中 Panel_Question.Controls.Add(rbl_Answers); //此处
- noor
抱歉,我无法格式化它,但我正在尝试将2个listItem添加到RadioButtoList,然后将其添加到面板中。 - noor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接