服务器控件创建子元素
我想知道如何为服务器控件创建子元素,是否有关于此的任何文档或教程,例如
<myMenu:Menu id="Menu1" runat="server">
<myMenu:MenuItem Text="Some Text" Link="www.msdn.com"> // <--
// ^-- Theis part how do you create child controls collection
</myMenu:MenuItem>
</myMenu:Menu>
namespace ControlsBook2Lib.Ch08
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Menu runat=server></{0}:Menu>")]
public class Menu : WebControl
{
protected override void RenderChildren(HtmlTextWriter writer)
{
base.RenderChildren(writer);
}
}
[ToolboxData("<{0}:MenuItem runat=server></{0}:MneuItem>")] <-- this part is wrong I know
public class MenuItem : WebControl
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
}
I want to know how do you create child elements for a server control and is there any documentation or tutorials on this e.g
<myMenu:Menu id="Menu1" runat="server">
<myMenu:MenuItem Text="Some Text" Link="www.msdn.com"> // <--
// ^-- Theis part how do you create child controls collection
</myMenu:MenuItem>
</myMenu:Menu>
namespace ControlsBook2Lib.Ch08
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Menu runat=server></{0}:Menu>")]
public class Menu : WebControl
{
protected override void RenderChildren(HtmlTextWriter writer)
{
base.RenderChildren(writer);
}
}
[ToolboxData("<{0}:MenuItem runat=server></{0}:MneuItem>")] <-- this part is wrong I know
public class MenuItem : WebControl
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在父控件中公开一个包含子项集合的属性 - 请注意,子类型不必是控件(它可以是常规类)。
请参阅这篇 MSDN 文章,其中介绍了如何开发此类服务器控件 - 请参阅示例,其中示例控件保存联系人类的集合。您还有一个开发集合编辑器的示例,以提供用于编辑集合编辑器的 UI。
You need expose a property in parent control that holds collection of child items - note that child type need not be a control (it can be a regular class).
See this MSDN article that describes how to develop such server control - see the example where a sample control holds collection of contact class. You also have an example of developing collection editor to provide UI for editing the same.