Saturday, May 7, 2011

Make an ASP.NET control property available in the mark-up



Hi Folks,


Just a few days ago it so happened that I was discussing with my colleague about the properties an ASP.NET control allows us to set in the code behind but not in the mark-up. We knew very well that there are some properties which we usually access in the code behind. Oh I should not use the word usually. In fact we mandatorily access some properties in the code behind and not in the mark-up because such properties are  not available in the mark-up at all.

The strange thing here is that we knew very well that certain properties like “SelectedValue” property of “DropDownList” control in ASP.NET are not available in the mark-up. You however can use these properties very well in the mark-up. Then what do I mean when I say “not available in the mark-up”? I mean that the intellisense won’t show such properties in the mark-up. Still if you assign a value to such a property, it works, it does work and this is probably because the value gets rendered properly to the control at runtime. This in turn is because the property even though not shown to us by intellisense in the mark-up is available in the class definition of the control.

You can even use Data-Binding expression to assign a value to the “SelectedValue” property of the “DropDownList” control.

So what is it that makes a property available in the code behind and not in the mark-up? Well it simply happens to be an attribute “[Browsable(false)]”. If a control-property is declared as “[Browsable(false)]” it will not be available in the mark-up and if a control-property is declared as “[Browsable(true)]” it will become available in the mark-up. The default value is true. I am not saying true just like that. In my code samples below I shall be showing how I inherited the classes “TextBox” and “DropDownList” and made a “browsable” property “unbrowsable” and an “unbrowsable” property “browsable”. Also, I shall be showing how a new property is by default available in the mark-up i.e. is by default set to “[Browsable(true)]”.

So I followed the below mentioned simple steps-



 1.       Created two CustomControls “MyCustomTextBox” and “MyCustomDropDownList” in my application.





2. Then I overrode a property for textbox and created  a new property “TestProperty” with no attributes defined for it  



3.       Then I overrode the SelectedValue property for DropDownList –


4.       Registered the controls in the web.config -




 5.       Now in the intellisense, see what happens. Let us first see the text box for the “TextMode” property and the new “TestProperty” property in the CustomControl “MyCustomTextBox
So, in the custom control, the property “TextMode” is not available because #2. And we see that the new property “TestProperty” is available. We did not specify the attribute “[Browsable(false)]” for this property. So it is by default browsable i.e. by default “[Browsable(true)]



6.       And in the ASP.NET TextBox control, the property TextMode is very well available in the mark-up



7.       Let us now see what’s up with DropwDownlist control. In out custom control, the property SelectedValue is available –




8.       And in the ASP.NET DropDownList control the property “SelectedValue” is not available


So friends that was pretty much good enough about the “[Browsable()]” attribute. There are number of other attributes like this which affect the behaviour of the ASP.NET controls. We generally don’t need to change or set any attribute for a Custom Control unless we sit and think something like “Why does the intellisense not show me the SelectedValue property for the DropDownList control in the mark-up even though a value set for this property in the mark-up works great”, like I did sitting in the office and a little bit of pondering over it led to this.

There are a number of other attributes for the controls which you can change as per your need in your custom control. One other such attribute is “EditorBrowsable” which probably allows configuring whether you want to see this property in the Properties window or not.

Suggestions, corrections, replies, responses and even any other new things are most welcome for this post. And yeah...  appreciation (if you feel so, ha ha ) is welcome too.

-Ankur