我有奇怪的问题..。:/
我的页面上有PlaceHolder,动态地生成更多的PlaceHolders。我已经存储了这个动态创建的PlaceHolders的名称。当我试图找到这个PlaceHolders中的任何一个时,我得到了对一个对象的错误空引用。
请帮助!:)
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;
}发布于 2017-03-05 14:13:19
我把代码改成了这个,它现在起作用了:
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;
}https://stackoverflow.com/questions/35409261
复制相似问题