简体   繁体   English

如何在标记中添加项目以供用户控制?

[英]How can add items in markup for user control?

I have a user control that generates a series of arrow buttons. 我有一个用户控件,生成一系列箭头按钮。 I want each button to display a custom user control. 我希望每个按钮都显示一个自定义用户控件。 My custom arrow button navigation control looks like this when rendered: 渲染时,我的自定义箭头按钮导航控件如下所示:

When complete, clicking a button will display a user control below the arrows. 完成后,单击按钮将在箭头下方显示用户控件。 Each arrow button needs to be tied to a custom user control containing unique functionality specific to that button. 每个箭头按钮都需要绑定到包含特定于该按钮的唯一功能的自定义用户控件。 Currently I have a simple List<ArrowTab> Tabs { get; set; } 目前我有一个简单的List<ArrowTab> Tabs { get; set; } List<ArrowTab> Tabs { get; set; } List<ArrowTab> Tabs { get; set; } property on the user control, and in the page_load of the page hosting the user control I add some sample tabs (seen above) programmatically, like this: List<ArrowTab> Tabs { get; set; }上的用户控件的属性,并在承载用户控制I添加一些样品片(上方观察)编程,这样在页面的Page_Load:

            TabWizard1.Tabs = new List<ArrowTab>
            {
                new ArrowTab
                {
                    Text = "Project Settings",
                    Selected = true
                },
                new ArrowTab { Text = "Groups" },
                new ArrowTab { Text = "Products" },
                new ArrowTab { Text = "Make Assignments" },
                new ArrowTab { Text = "Review Assignments" },
                new ArrowTab { Text = "Commitments" }
            };

This works for adding the button from the code behind but what I actually want is to add the button in the markup for the user control on the page and also associate a different custom user control to that button. 这适用于从后面的代码中添加按钮,但我真正想要的是在页面上为用户控件添加标记按钮,并将不同的自定义用户控件与该按钮相关联。 For example, I want to do something like this: 例如,我想做这样的事情:

    <uc:TabWizard ID="TabWizard1" runat="server">
        <items>
            <arrowTab Text="Project Settings">
                <uc:ProjectSettingsControl ID="projectSettings1" runat="server" />
            </arrowTab>
            <arrowTab Text="Groups">
                <uc:GroupsControl ID="groups1" runat="server" />
            </arrowTab>
            <arrowTab Text="Products">
                <uc:ProductsControl ID="products1" runat="server" />
            </arrowTab>
            <arrowTab Text="Make Assignments">
                <uc:MakeAssignmentsControl ID="makeAssignments1" runat="server" />
            </arrowTab>
            <arrowTab Text="Review Assignments">
                <uc:ReviewAssignmentsControl ID="reviewAssignments1" runat="server" />
            </arrowTab>
            <arrowTab Text="Commitments">
                <uc:CommitmentsControl ID="commitments1" runat="server" />
            </arrowTab>
        </items>
    </uc:TabWizard>

An example of someone already doing something similar is the Telerik RadPanel control. 已经做类似事情的人的一个例子是Telerik RadPanel控件。

<telerik:RadPanelItem Text="Mail" ImageUrl="Img/mail.gif" Expanded="True">
    <Items>
        <telerik:RadPanelItem ImageUrl="Img/mailPersonalFolders.gif" Text="Personal Folders" />
        <telerik:RadPanelItem ImageUrl="Img/mailDeletedItems.gif" Text="Deleted Items" />
        <telerik:RadPanelItem ImageUrl="Img/mailInbox.gif" Text="Inbox" />
        <telerik:RadPanelItem ImageUrl="Img/mailFolder.gif" Text="My Mail" />
        <telerik:RadPanelItem ImageUrl="Img/mailSent.gif" Text="Sent Items" />
        <telerik:RadPanelItem ImageUrl="Img/mailOutbox.gif" Text="Outbox" />
        <telerik:RadPanelItem ImageUrl="Img/mailSearch.gif" Text="Search Folders" />
    </Items>
</telerik:RadPanelItem>

I have no idea how to create a user control that allows this behavior. 我不知道如何创建允许此行为的用户控件。 Even just a nudge in the right direction would be beneficial. 即使只是朝着正确的方向轻推也是有益的。

The link Richard provided should point you in the right direction, but to answer some of your questions: 理查德提供的链接应指向正确的方向,但要回答您的一些问题:

How do I allow child elements? 我如何允许子元素?

You can make a server control support child elements by marking the property with the PersistenceMode and DesignerSieralizationVisibility attributes. 您可以通过使用PersistenceModeDesignerSieralizationVisibility属性标记属性来使服务器控件支持子元素。 In your case, this property would be a collection of elements, like in the example below: 在您的情况下,此属性将是元素的集合,如下例所示:

    /// <summary>
    /// Gets the columns collection.
    /// </summary>
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public GridColumnCollection Columns
    {
        get
        {
            if (columnsCollection == null)
            {
                if (columnsArrayList == null)
                {
                    this.EnsureChildControls();
                    if (columnsArrayList == null)
                        columnsArrayList = new ArrayList();
                }
                columnsCollection = new GridColumnCollection(columnsArrayList);
            }
            return columnsCollection;
        }
    }

How do I iterate over those elements in page load the user control? 如何在页面中迭代这些元素加载用户控件?

The child element collection will be mapped to a property, and you can iterate over the collection to add the elements to your control. 子元素集合将映射到属性,您可以迭代集合以将元素添加到控件。

foreach (GridColumn dataColumn in columnsCollection)
{
    var cell = new GridCell(dataColumn.HeaderStyle, dataColumn.HeaderText);
    item.Cells.Add(cell);
}

Do child items need to be user controls as well? 子项目也需要是用户控件吗?

No, child items will be other server controls. 不,子项将是其他服务器控件。 Your items property will map to a collection of server controls, which in your case are the ArrowTab controls. 您的items属性将映射到服务器控件的集合,在您的情况下是ArrowTab控件。

And finally, if the child items need to be their own user control then do those controls need some sort of ContentTemplate thing? 最后,如果子项需要是他们自己的用户控件,那么这些控件是否需要某种ContentTemplate?

Yes, you would want to implement the ITemplate interface in your ArrowTab server control. 是的,您可能希望在ArrowTab服务器控件中实现ITemplate接口。 The link that Richard provided should explain how to do this. Richard提供的链接应该解释如何执行此操作。

我相信你所追求的是构建模板化的自定义ASP.NET服务器控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM