【问题标题】:Finding controls in placeholder在占位符中查找控件
【发布时间】:2011-04-04 18:50:56
【问题描述】:

我正在使用 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 添加到占位符?

标签: c# asp.net ajax visual-studio user-controls


【解决方案1】:

试试这个代码:

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: 你应该写 Page 或者这个
  • 你的意思是这个功能会在.ascx页面吗?
  • @noor:这个函数是静态的,可以放在任何地方
【解决方案2】:

编辑:

   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添加到控件内的面板时会引发异常。
  • 对象引用未设置为对象异常的实例。
  • 你能把用户控制代码中出现这个异常的部分贴出来吗?
  • 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_Answer.Items.Add(rb_Answer_False); //添加 RadioButtonList tp 面板 Panel_Question.Controls.Add(rbl_Answers); //这里
  • 对不起,我无法格式化,但我正在尝试将 2 个 listItems 添加到 RadioButtoList,然后将其添加到面板中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2019-02-23
相关资源
最近更新 更多