我正在 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" />
关于c# - 如何获取带有破折号的服务器控件属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7507615/