1

我可以从其他人的控件中看到,可以在标记中设置子对象属性。例如,如果我使用 Telerik 的 RadComboBox,我可以写...

<telerik:RadComboBox runat="server" ID="RadComboBox2">
    <CollapseAnimation Duration="100" />
</telerik:RadComboBox>

或者,我也可以写...

<telerik:RadComboBox runat="server" ID="RadComboBox2" CollapseAnimation-Duration="100">
</telerik:RadComboBox>

我必须采用什么技术来允许我使用我编写的控件来做到这一点?我认为我可能必须在我的父控件中为我公开的子对象的每个属性显式创建属性。但是,我似乎不允许创建名称中带有“-”的属性。

4

1 回答 1

3

试试这个:

1 - 属性类定义

public class Option
{
    public string First { get; set; }
    public string Last { get; set; }
}

2 - 用户控件定义

public partial class CustomUC : System.Web.UI.UserControl
{
    //Enables the Option properties to be filled inside the control's tag
    [PersistenceMode(PersistenceMode.InnerProperty)] 
    //Enables the Option properties to be filled on the control's tag
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Option Options
    {
        get;set;
    }

    protected void Page_Load(object sender, EventArgs e) { }
}

3 - 标记:

<own:CustomUC ID="uc" runat="server" Options-First="First" Options-Last="Last" />

或者

<own:CustomUC ID="uc" runat="server" >
   <Options-First="First" Options-Last="Last" />
</own:CustomUC>

注意:您必须首先使用您自己的 tagPrefix 引用用户控件程序集。

于 2013-01-25T12:09:46.440 回答