Design-Time Attributes of Custom Controls
 
Published: 29 Apr 2004
Unedited - Community Contributed
Abstract
If you have ever worked in Visual Studio.NET, you will have noticed that all of the server controls have unique properties to all of them that you can change in the properties window. This functionality is provided by attributes in the control itself. This article will go over how to provide the same functionality with your custom controls.
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30170/ 81

Introduction

Introduction

In previous articles, I have shown you how to create and customize Custom Controls and this article will expand on that. If you have ever worked in Visual Studio.NET, you will have noticed that all of the server controls have unique properties to all of them that you can change in the properties window. This functionality is provided by attributes in the control itself. This article will go over how to provide the same functionality with your custom controls.

The Base Control

The base control

The control that will be used for this article will the same one that we used in Posting Back in Custom Controls (Single Highlight). However, first we will make some changes to it -

When you inherit from System.Web.UI.WebControls.WebControl you automatically get a bunch of properties that by default already show up in VS.NET (including BackColor, BorderStyle, Font, CssClass etc.) which you can chose to implement of not (you could also inherit from System.Web.UI.Control and not get any of those properties) . In our new highlighting class (VSHighlight) we will be using some of these properties.

We are also going to need some additional properties that aren't initially provided by the base class (eg. the Text of the control and the alternating background color).

Below is the new class -

Public Class VSHighlight
Inherits
System.Web.UI.WebControls.WebControl
Implements
IPostBackEventHandler

Private pvText As String
Private
tColor As System.Drawing.Color = System.Drawing.Color.Black
Private
bgColoron As System.Drawing.Color = System.Drawing.Color.LimeGreen

Public
Property TextColor() As System.Drawing.Color

Get

Return
tColor

End
Get
Set
(ByVal Value As System.Drawing.Color)
tColor = Value

End
Set
End
Property

Public Property BackColor_On() As System.Drawing.Color
Get

Return
bgColoron

End
Get
Set
(ByVal Value As System.Drawing.Color)
bgColoron = Value

End
Set
End
Property

Public Property Text() As String
Get

Return
pvText

End
Get
Set
(ByVal Value As String)
pvText = Value

End
Set
End
Property

Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
output.AddAttribute("border", "1")
output.AddAttribute("onClick", Page.GetPostBackEventReference(Me))

If
Viewstate(ID & "click") = 1
Then
output.AddAttribute("bgcolor", BackColor_On.ToKnownColor.ToString())

Else

output.AddAttribute("bgcolor", MyBase.BackColor.ToKnownColor.ToString())

End
If
output.AddStyleAttribute("cursor", "hand")
output.AddStyleAttribute("border-width", MyBase.BorderWidth.Value.ToString())
output.AddStyleAttribute("border-color", MyBase.BorderColor.ToKnownColor.ToString())
output.AddStyleAttribute("height", "5")
output.AddStyleAttribute("border-style", MyBase.BorderStyle.ToString())
output.RenderBeginTag("table")
output.RenderBeginTag("tr")
output.RenderBeginTag("td")
output.AddAttribute("face", "Arial")
output.AddAttribute("size", "4")
output.RenderBeginTag("font")
output.RenderBeginTag("b")
Page.Response.Write(Text)
output.RenderEndTag()
output.RenderEndTag()
output.RenderEndTag()
output.RenderEndTag()
output.RenderEndTag()

End
Sub

Public Sub RaisePostBackEvent(ByVal eventargs As String) _
Implements
IPostBackEventHandler.RaisePostBackEvent

If
ViewState(ID & "click") = 1
Then
ViewState(ID & "click") = 0

Else

ViewState(ID & "click") = 1

End
If
End
Sub

End Class

Attributes

Attributes

If you have no idea what I mean by "Attributes" then -

Attributes are much like keywords (like Public and Private that you can add to classes, methods etc.) in the sense that they tell the compiler more information about your class, method, property, event etc. For example, in this article we will be using attributes like Description which provide a description of the property so that any other application (in this case VS.NET) can read that attribute for that property and act on it.

There are a variety of attributes that are available for all sorts of things, most of them are provided by the System.ComponentModel namespace which provides you with classes that can be used to alter run-time and design-time behavior of controls. Simple? Let's go.

Altering the properties

Altering the properties

Because this is a very simple overview I'll just do a couple of the properties, but you will quickly get the idea.

There are a variety of attributes that are available for you to use for your properties (since attributes in VS.NET are mainly only for properties and events) and the most common are summarized in the following table -

Attribute  Description
BrowsableAttribute Specifies weather the property or event is displayed in the property window or not.
CategoryAttribute Specifies the category to put the property or event under (can be an existing or a new category).
DescriptionAttribute Specifies the description text for the property or event.
DefaultPropertyAttribute Specifies the default property that is selected in the property window when the control is selected. You set this attribute for the class, not the property.

There are many other attributes that you can use which you can see here.

Revising the control

Revising the Control

Below is the updated control (not all of it because the missing part was exactly the same).

<DefaultProperty("Text")> _
Public
Class VSHighlight
Inherits
System.Web.UI.WebControls.WebControl
Implements
IPostBackEventHandler

Private pvText As String
Private
tColor As System.Drawing.Color = System.Drawing.Color.Black
Private
bgColoroff As System.Drawing.Color = System.Drawing.Color.White
Private
bgColoron As System.Drawing.Color = System.Drawing.Color.LimeGreen

<TypeConverter(GetType(System.Drawing.ColorConverter)), _
Category("Appearance"), _
Description("Sets the color of the text.")> _
Public
Property TextColor() As System.Drawing.Color

Get

Return
tColor

End
Get
Set
(ByVal Value As System.Drawing.Color)
tColor = Value

End
Set
End
Property

<TypeConverter(GetType(System.Drawing.ColorConverter)), _
Category("Appearance"), _
Description("Sets the background color of the table when clicked.")> _

Public
Property BackColor_On() As System.Drawing.Color
Get

Return
bgColoron

End
Get
Set
(ByVal Value As System.Drawing.Color)
bgColoron = Value

End
Set
End
Property

<Category("Misc"), _
Description("Specifies the text to be displayed."), _
DefaultValue("Sample Text"), _
Browsable(True)> _

Public
Property Text() As
String
Get

Return
pvText

End
Get
Set
(ByVal Value As String)
pvText = Value

End
Set
End
Property

Pretty easy right?

Finishing Up

In VS.NET

If you add the control to VS.NET and look in the property window, you should see something like -

Summary

Here we have gone through the process of adding some attributes to your custom controls to provide more functionality in VS.NET. There are a lot more attributes that you can add to your properties and events (even though events weren't covered here). There is also a lot more functionality that you can add to your controls in VS.NET which I will uncover in future articles.



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-03-05 2:49:12 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search