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

标签 c# asp.net webforms custom-server-controls

我正在 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/

相关文章:

c# - 记住单个页面上的状态时,使用隐藏字段总是比 session 更好吗?

javascript - ASP.Net - 在错误页面上隐藏子菜单

c# - 在 C# ASP.net 中将 ALPHA-2 转换为 ALPHA-3

C# 打印属性

c# - 在 C# 中使用函数按字段名称返回数据库值索引

asp.net - 如何为客户端和服务器缓存设置不同的缓存过期时间

javascript - 即使在页面刷新后,从 Javascript 设置的隐藏字段值也会保留

c# - 当我需要传入 HttpResponse 时,如何测试第三方框架

c# - 如何在 .NET Webforms 中使用 Jquery AJAX 将数据发布到 MySQL

c# - ASP.NET Identity 2在保存扩展应用程序用户时插入重复数据或保存两次