开发者

Finding controls in placeholder

i'm using AJAX to make a user control which contains a panel that contains label and RadioButtonList or CheckBoxList according to a condition. There is a placeholder in .aspx page where that control should be in. I need to find List from placeholder I tried this:

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

    }

When I use break points, I found that Controls property of p is 0.

Note: ChildRadioButtonList is a property in the QuestionPanelControl. Any suggestions...


try this code:

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


EDIT:

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

    }

and see if the Controls.Count for the placeholder is something other than 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜