The default value in asp.net server management

I have a problem with the default attribute.

When I add my control to the page in design mode, the 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 in design mode, when I add a control from the toolbar, the key does not exist

 <cc1:KHTLabel ID="KHTLabel1" runat="server"></cc1:KHTLabel> 
+4
source share
3 answers

This is not what the [DefaultValue] attribute does, I'm afraid. What he does is let the designer of the Visual Studio (in particular, the "Properties" grid) determine what to show by default, and therefore know how to show the value in bold if it differs from the standard one.

You need to have something in your code that has the value "KHT" as the default value. There is some important information in this blog in 2008 .

The following code is pretty rudimentary, and I could not verify its compilation, but it should give you an idea of ​​how you could handle the value of your DefaultValueAttribute in 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"); } 
+4
source

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

+2
source

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

By the way, I don’t understand why you currently have key=dfd in ToolboxData , and the name of the Key property 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 } } 
+1
source

All Articles