【问题标题】:Trouble when adding control dynamically动态添加控件时出现问题
【发布时间】:2014-06-02 17:14:10
【问题描述】:

我的页面包含“基本”用户控件。这个用户控件需要动态加载用户控件。我为测试编写了这些控件之一,它被称为ContentTemplate,它的类型是ContentTemplateType。 在“基本”用户控件的 Page_Load 事件中,我有下一个代码:(ph - asp:PlaceHolder)

ph.Controls.Add(new LiteralControl(String.Format("<!-- ko if: {0} -->", cond)));
ph.Controls.Add(new ContentTemplate());
ph.Controls.Add(new LiteralControl("<!-- /ko -->"));

但最后在页面上我只有&lt;!-- ko if: live == false --&gt;&lt;!-- /ko --&gt; 行。 我也试过下一个代码:

ph.Controls.Add(Page.LoadControl(ContentTemplateType, null));

但结果相同。 我做错了什么?

【问题讨论】:

  • 不清楚你想达到什么目标,你能更简单地告诉我们你想达到什么目标,什么没有发生吗?
  • 我希望在&lt;!-- ko --&gt; 标签之间呈现ContentTemplate usercontrol

标签: c# asp.net user-controls


【解决方案1】:

请使用这个

Control ctrl = Page.LoadControl("UserControlPath");

ph.Controls.Add(ctrl);

更多详情请访问Template Control Load

【讨论】:

  • 我不想使用这个重载,这取决于路径。我尝试在第一个参数是控制类型的情况下使用重载,但它不起作用。
猜你喜欢
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多