简体   繁体   中英

Accessing sub-object properties in markup

I can see from other people's controls that it is possible to set sub-object properites in markup. For example, if I'm using Telerik's RadComboBox I can write ...

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

or, alternatively I can write...

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

What technique do I have to emply to allow me to do this with controls I write? I thought that I might have to explicitly create properties in my parent control for each of the of properties my sub-object I expose. However, I don't seem to allowed to create a property with a '-' in the name.

Try this:

1 - Property Class Definition

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

2 - UserControl Definition

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 - Markup:

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

or

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

Note: You have to reference the usercontrol assembly first with your own tagPrefix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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