【问题标题】:Accessing sub-object properties in markup访问标记中的子对象属性
【发布时间】:2013-01-25 10:22:47
【问题描述】:

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

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

或者,我也可以写...

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

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

【问题讨论】:

    标签: c# asp.net user-controls telerik


    【解决方案1】:

    试试这个:

    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 引用用户控件程序集。

    【讨论】:

    • 酷!太好了,我让它工作了。正如您的帖子所建议的那样,我现在可以写Options-First="First",但似乎需要额外的魔法才能让我拥有一个内部内容标签,这样我才能写&lt;Options First="First"/&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多