我正在使用AJAX来创建一个用户控件,其中包含一个包含label和RadioButtonList或CheckBoxList的面板。 在.aspx页面中有一个占位符,该控件应该位于该位置。 我需要从占位符中找到List 我试过这个:
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中的一个属性。 任何建议......
答案 0 :(得分: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;
}
}
答案 1 :(得分: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以外的其他内容。