Thursday, December 4, 2014

Browsable / DesignerSerializationVisibility / EditorBrowsable

There are a lot of Attributes that come to help us all along when we develop custom controls for ASP.NET.

Here are a few important ones that would perform some basic operations for us during development.

These are more useful at design time then actual render time.

//This attribute when true shows up the property in the Properties Grid for a control[Browsable(true)] 
//This attribute sets whether the property would be available to VisualStudio Code Editor Intellisense.[EditorBrowsable(EditorBrowsableState.Never)] 
//This attribute sets whether the property would be serialized in the Designer HTML View or not.[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

Explore the options available with these enums and their effects to know more on those attriutes.

IMP - There could be times when changing these attributes would not have any effect. 

Keep Calm :) .

This could be because of Visual Studio caching the intellisense schema and displaying the cached schema for you.

Simply clear the cache by deleting all the files in 
"C:\Documents and Settings\[YOUR_USER_NAME]\Application Data\Microsoft\VisualStudio\10.0\ ReflectedSchemas"

or whereever your schema files are located.

Further, another important aspect is that even when you do all this, the added custom property would be available to you at design time as well as code behind. However this property would not be rendered with the page and as a result, it would not be available at the client side for any operation.

Usually this is not required but in case you need that property to be available to you client side as an attribute, all you need to do is overwrite the AddAttributesToRender method.

// Overriding the AddAttributesToRender Method because we need to add the custom       
// properties and there values to its control html so we can use access them on        
// Client Side using JavaScript        
protected override void AddAttributesToRender(HtmlTextWriter writer)        
             writer.AddAttribute("PropertyName", this.PropertyName);
            // Calling Base Class AddAttributesToRender Method                 

No comments:

Post a Comment