查找占位符另一个占位符
本文关键字:占位符 另一个 查找 | 更新日期: 2023-09-27 18:34:35
我有奇怪的问题... :/
我在我的页面上有占位符,在那里我动态生成更多的占位符。我已经存储了这个动态创建的占位符的名称。当我尝试找到任何这个占位符时 - 我有一个对象的错误空引用。
请帮忙! :)
private void btnMoreInfo_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string[] componentName = button.ID.Split('_');
String controlName = null;
foreach (String singlePlaceHolder in placeHolderNames)
{
if (singlePlaceHolder.Contains(componentName[0]))
controlName = singlePlaceHolder;
}
Control cph = this.Master.FindControl(controlName);
Label helperlabel = new Label();
helperlabel.Text = "That one!";
cph.Controls.Add(helperlabel);
cph.Visible = true;
}
我将代码更改为此代码,它现在可以工作了:
var cph = FindControlRecursive(this.Master, controlName);
加上我添加了新方法:
private Control FindControlRecursive(Control root, string id)
{
if(root.Id==id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t= FindControlRecursaive(c, id);
if (t !=null)
{
return t;
}
}
return null;
}