gpt4 book ai didi

c# - 在占位符中查找控件

转载 作者:行者123 更新时间:2023-11-30 16:30:37 25 4
gpt4 key购买 nike

我正在使用 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 中的一个属性。任何建议...

最佳答案

试试这段代码:

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

关于c# - 在占位符中查找控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5542901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com