2011-09-21 1 views
1

Je crée un contrôle de serveur personnalisé dans ASP.NET WebForms et souhaitez avoir un trait d'union dans mon nom de propriété de la même manière que les contrôles serveur ASP.NET dans le balisage. Par exemple, un contrôle Label possède une propriété « Font-Size » dans le balisage comme ceci:Comment faire pour obtenir un nom de propriété de contrôle de serveur avec un tiret

<asp:Label ID="Label1" Font-Size="small" Text="hi" runat="server" /> 

Comment y parvenir?

Répondre

2

Il suffit d'utiliser les propriétés complexes sur votre contrôle:

public class MyControl: WebControl 
{ 
    public Test() 
    { 
     // Make sure to initialize the complex property or you will get a NRE 
     // when you try to set the Complex-Bar property in the webpage 
     Complex = new Complex(); 
    } 

    public Complex Complex { get; set; } 
} 

public class Complex 
{ 
    public string Bar { get; set; } 
} 

puis:

<asp:MyControl runat="server" ID="myControl" Complex-Bar="foo bar" /> 
+0

J'ai essayé ceci dehors et cela fonctionne. Savez-vous comment faire apparaître IntelliSense en utilisant cette approche? – Halcyon

0

j'ajouté ce qui suit pour obtenir IntelliSense travailler avec la propriété complexe:

[Category("Appearance")] 
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
[PersistenceMode(PersistenceMode.InnerProperty)] 
Questions connexes