default value in asp.net server control
Asked Answered
D

3

5

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>
Dissatisfaction answered 7/9, 2010 at 8:46 Comment(0)
C
4

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");
}
Comanchean answered 7/9, 2010 at 8:50 Comment(4)
thnks for reply , it's work but only at design mode , is there any way that my property default value add to control tag like this : <cc1:KHTLabel ID="KHTLabel1" Key="KHT" runat="server"></cc1:KHTLabel>Dissatisfaction
@Dissatisfaction - The DefaultValue attribute won't do that for you, and there's, nothing I'm aware of that will do that. It's not what it's for and is quite redundant as it would result in duplicated valuesComanchean
@Dissatisfaction - I'm just wondering, if the value is the default, why do you want to see it in your markup? =)Comanchean
it is more friendly to other developers that use this control :)Dissatisfaction
E
2

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.

Emogeneemollient answered 7/9, 2010 at 8:58 Comment(0)
S
1

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
    }
}
Steatopygia answered 19/11, 2011 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.