【问题标题】:Find PlaceHolder another PlaceHolder查找 PlaceHolder 另一个 PlaceHolder
【发布时间】:2016-05-26 08:33:59
【问题描述】:

我有一个奇怪的问题...:/

我的页面上有 PlaceHolder,我在其中动态生成更多 PlaceHolder。 我已经存储了这个动态创建的 PlaceHolders 的名称。 当我尝试查找其中任何一个 PlaceHolders 时 - 我有一个对象的错误 null 引用。

请帮忙! :)

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

【问题讨论】:

  • 此处代码中断:Control cph = this.Master.FindControl(controlName);

标签: c# .net dynamic controls


【解决方案1】:

我把代码改成了这个,它现在可以工作了:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2015-08-14
    • 2016-01-28
    • 2011-11-24
    • 1970-01-01
    • 2012-07-12
    • 2013-05-23
    相关资源
    最近更新 更多