Skip to main content
 首页 » 编程设计

c#之如何获取带有破折号的服务器控件属性名称

2024年02月23日17lhb25

我正在 ASP.NET WebForms 中创建一个自定义服务器控件,并希望在我的属性名称中有一个连字符,就像 ASP.NET 服务器控件在标记中所做的那样。例如,Label 控件在标记中有一个“Font-Size”属性,如下所示:

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

我该如何实现?

请您参考如下方法:

只需在控件上使用复杂的属性:

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; } 
} 

然后:

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