首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用LayoutTemplate实现PlaceHolder

如何用LayoutTemplate实现PlaceHolder
EN

Stack Overflow用户
提问于 2011-01-06 23:02:01
回答 2查看 2.2K关注 0票数 0

在我自己的服务器控件中,我希望实现类似于ListView的东西。

代码语言:javascript
复制
<asp:ListView runat="server">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" id="itemPlaceholder" />
    </LayoutTemplate>
</asp:ListView>

我已经创建了一个ITemplate属性,我可以在aspx页面中设置布局,并且正在执行ITemplate.InstantiateIn(myControl)

但我不知道如何在占位符处插入控件。我猜它会像MyTemplate.FindControl("itemPlaceholder").Controls.Add(myControl)一样。我尝试转换为我的ITemplate类型,但是我得到了错误:

代码语言:javascript
复制
Unable to cast object of type 'System.Web.UI.CompiledTemplateBuilder' to type 'MyNamespace.MyLayoutTemplate'

我遗漏了什么?

编辑:,我刚找到这个:http://www.nikhilk.net/SingleInstanceTemplates.aspx Control developers can define templates to be single instance using metadata which causes the ID'd controls within the template contents to be promoted to the page level... The parser and code-generator together work behind the scenes to add member fields, and initialize them at the right time.。它似乎只适用于用户控件?做完这件事后,我试过Page.FindControl(),但没有找到任何东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-06 23:27:47

好的,这只是用户错误的问题。我在一个ITemplate中实例化了Panel,所以很明显,Page.FindControl() (不是递归的)不能工作。一旦我做了Panel.FindControl(),一切都成功了。

票数 1
EN

Stack Overflow用户

发布于 2011-01-06 23:12:55

在过去,我使用过这个库和sucess http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4620918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档