有没有把一个ASP.NET自定义控件内侧内控制的方法吗? [英] Is there a way to put inner controls inside a ASP.NET Custom Control?

查看:94
本文介绍了有没有把一个ASP.NET自定义控件内侧内控制的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要像做(更新为例):

I want to do something like (Updated example):

<uc:Tabs>
  <Tab Name="A handy tab">
    <Node Url="~/Default.aspx" />
    <Node Url="~/Node2.aspx" />
  </Tab>      
  <Tab Name="Another handy tab">
    <Node Url="~/Neato.aspx" />
    <Node Url="~/Node3.aspx" />
    <Node Url="~/Node4.aspx" />
  </Tab>
<uc:Tabs>

可能吗?任何教程或如何做的?我不知道该怎么甚至搜索或什么这就叫所以没有发现任何至今。内部控制?内蒙古集什么东西...?

Possible? Any tutorials or how-to's? I'm not sure what to even search on or what this is called so haven't found anything so far. Inner controls? Inner collection something something...?

推荐答案

使用的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.parsechildrenattribute.aspx\">ParseChildrenAttribute和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.persistchildrenattribute.aspx\">PersistChildrenAttribute属性:

[ParseChildren(false)]
[PersistChildren(true)]
public class MyControl : UserControl { }

这将导致任何控制你把里面的参考:

This will cause any controls you put inside the reference:

<uc:MyControl runat="server">
  <asp:TextBox runat="server" />
<uc:MyControl>

要附加到你的用户控件内容的控件集合的末尾。

To be appended to the end of the Controls collection of your UserControl contents.

不过,如果你想拥有的控件的集合,你应该使用一个服务器控件,而不是用户控件。对于工作原理是这样的控件:

However, if you want to have a collection of controls, you should probably use a server control and not a user control. For a control that works like this:

<foo:TabControl runat="server">
    <Tabs>
        <foo:Tab CssClass="myclass" Title="Hello World" />
    </Tabs>
</foo:TabControl>

您需要有一个标签属性的控件类;该标签属性应该是一个集合;它应该包含类型标签的对象。我创建了三个班在这里:

You need a Control class that has a Tabs property; the Tabs property should be a Collection; and it should contain objects of type Tab. I've created the three classes here:

[ParseChildren(true, "Tabs")]
public class TabControl: WebControl, INamingContainer
{
    private TabCollection _tabs;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public TabCollection Tabs
    {
        get
        {
            if (_tabs == null)
            {
                _tabs = new TabCollection();
            }
            return _tabs;
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        foreach (Tab tab in Tabs)
        {
            writer.WriteBeginTag("div");
            writer.WriteAttribute("class", tab.CssClass);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Write("this is a tab called " + tab.Title);
            writer.WriteEndTag("div");
        }
    }
}

和标签类:

public class Tab
{
    public string CssClass { get; set; }
    public string Title { get; set; }
}

和标签系列:

public class TabCollection : Collection<Tab> { }

这篇关于有没有把一个ASP.NET自定义控件内侧内控制的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆