ASP.NET 服务器控件中的默认值

发布于 2024-09-18 17:37:03 字数 793 浏览 9 评论 0原文

我对默认值属性有疑问。

当我在设计模式下将控件添加到页面时,默认值不起作用。这是我的代码:

[DefaultProperty("Text")]
[ToolboxData("<{0}:KHTLabel runat=server key=dfd></{0}:KHTLabel>")]
public class KHTLabel : Label ,IKHTBaseControl
{
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("KHT")]
    [Localizable(true)]
    public string Key
    {
        get
        {
            String s = (String)ViewState["Key"];
            return ((s == null) ? String.Empty : s);
        }

        set
        {
            ViewState["Key"] = value;
        }
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {......

但是,在设计模式下,当我从工具箱添加控件时,该键不存在

<cc1:KHTLabel ID="KHTLabel1" runat="server"></cc1:KHTLabel>

I have a problem with the default value attribute.

When I add my control to page at design mode, default value does not work. This is my code:

[DefaultProperty("Text")]
[ToolboxData("<{0}:KHTLabel runat=server key=dfd></{0}:KHTLabel>")]
public class KHTLabel : Label ,IKHTBaseControl
{
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("KHT")]
    [Localizable(true)]
    public string Key
    {
        get
        {
            String s = (String)ViewState["Key"];
            return ((s == null) ? String.Empty : s);
        }

        set
        {
            ViewState["Key"] = value;
        }
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {......

But, at design mode when I add a control from the toolbox, the key does not exist

<cc1:KHTLabel ID="KHTLabel1" runat="server"></cc1:KHTLabel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光下的微笑 2024-09-25 17:37:03

恐怕这不是 [DefaultValue] 属性的作用。它的作用是让 Visual Studio 设计器(特别是“属性”网格)确定默认显示的内容,以及如何知道将值显示为粗体 strong> 当它与默认值不同时。

您可以在代码中保留“KHT”值作为默认值,这取决于您。 此中有一些相关细节我的 2008 年博客文章

以下代码相当初级,我无法验证它是否可以编译,但它应该可以让您了解如何将 DefaultValueAttribute 的值“强制”到 视图状态

private string GetDefaultAttributeValueForProperty(string propertyName)
{
    var attributesForProperty = (from prop in typeof(KHTLabel).GetProperties()
                 where prop.Name == propertyName
                 select System.Attribute.GetCustomAttributes(prop)).First();
    var defaultValueAttribute = (from attr in attributesForProperty
                 where attr.GetType() == typeof(DefaultValueAttribute)
                 select ((DefaultValueAttribute)attr).Value).FirstOrDefault();

    return Convert.ToString(defaultValueAttribute);
}
public KHTLabel()
{
    ViewState["Key"] = GetDefaultAttributeValueForProperty("Key");
}

That's not what the [DefaultValue] attribute does, I'm afraid. What it does it allows the Visual Studio designer (specifically the "Properties" grid) to determine what to show by default and therefore how to know to show the value as bold when it's different from the default.

It's upto you to have something in your code that holds the value "KHT" as the default value. There's a bit of relevant detail in this 2008 blog posting of mine.

The following code is fairly rudimentary, and I haven't been able to verify it compiles, but it should give you some idea how you could handle "forcing" the value of your DefaultValueAttributes into the ViewState:

private string GetDefaultAttributeValueForProperty(string propertyName)
{
    var attributesForProperty = (from prop in typeof(KHTLabel).GetProperties()
                 where prop.Name == propertyName
                 select System.Attribute.GetCustomAttributes(prop)).First();
    var defaultValueAttribute = (from attr in attributesForProperty
                 where attr.GetType() == typeof(DefaultValueAttribute)
                 select ((DefaultValueAttribute)attr).Value).FirstOrDefault();

    return Convert.ToString(defaultValueAttribute);
}
public KHTLabel()
{
    ViewState["Key"] = GetDefaultAttributeValueForProperty("Key");
}
掐死时间 2024-09-25 17:37:03

DefaultValueAttribute 不用于设置属性的值。序列化器使用它来确定是否应该序列化该值。您需要在构造函数(或 OnInit)中设置属性的默认值。如果属性值与 DefaultValueAttribute 值匹配,则使用 DefaultValueAttribute 会使序列化数据更小。

The DefaultValueAttribute is not used to set the value for the property. It is used by the serializer to determine wheather it should serialize the value or not. You need to set your default value for the property in the constructor (or in OnInit). Using the DefaultValueAttribute makes the serialized data smaller if the property-value matches the DefaultValueAttribute value.

她如夕阳 2024-09-25 17:37:03

您可以获得您在第一个答案的评论中提到的结果 () 通过在 ToolboxDataAttribute 中显式命名它。
为了使其成为实际的默认值,您仍然必须在属性的 getter 中返回该值。这会导致在您的类中将相同的值重复 3 次。

顺便说一句,我不明白为什么您当前的 ToolboxData 中有 key=dfd,而属性名称是 Key 并且是字符串类型。

[DefaultProperty("Text")]
[ToolboxData("<{0}:KHTLabel runat=server Key=\"KHT\"></{0}:KHTLabel>")]
public class KHTLabel : Label//, IKHTBaseControl
{
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("KHT")]
    [Localizable(true)]
    public string Key
    {
        get
        {
            var s = (String)ViewState["Key"];
            return (s ?? "KHT");
        }

        set { ViewState["Key"] = value; }
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        // TODO: Implement logic
    }
}

You can get the result you mentioned in your remark on this first answer (<cc1:KHTLabel ID="KHTLabel1" runat="server" Key="KHT"></cc1:KHTLabel>) by explicitly naming it in the ToolboxDataAttribute.
To make this also be the actual default value, you still have to return that value in the getter of the property. It results in duplicating the same value three times in your class.

BTW, I don't understand why you currently have key=dfd in your ToolboxData, while the property name is Key and is of type string.

[DefaultProperty("Text")]
[ToolboxData("<{0}:KHTLabel runat=server Key=\"KHT\"></{0}:KHTLabel>")]
public class KHTLabel : Label//, IKHTBaseControl
{
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("KHT")]
    [Localizable(true)]
    public string Key
    {
        get
        {
            var s = (String)ViewState["Key"];
            return (s ?? "KHT");
        }

        set { ViewState["Key"] = value; }
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        // TODO: Implement logic
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文