Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
ASP Insider

Microsoft Store

Creating a Common Copyright Control
With all the cool new things going on in the .NET space some of the fundamental tings can get lost on us. Over the past few months I have decided to tackle what I think is sort of a forgotten aspect of ASP.NET and that is creating Web Controls. When I think about creating ASP.NET web controls I think of one developer, Miguel Castro AKA DataDude. Miguel recently spent some time with Carl and Richard on DotNetRocks talking about Web Controls in ASP.NET 2.0. I think that episode sort of gave me some inspiration to start looking at ways I could roll my own web controls. I soon began writing some controls to handle some of the common aspects of just about every web site I create (and I think I have created about 300 .NET sites and applications in the past 6 years).

CopyRight Control PropertiesMy first entry today will be a very simple control and will display copyright information you commonly see at the bottom of every page. Sounds pretty simple, but think about it, we generally use this on every site we develop don't we? This control will most likely be used on just about every site I create from now on and save me a few minutes of time on each project. Think about it if I create 100 base pages or master pages in a year, which is not uncommon for me, and saving 5 minutes a page that is over 8 hours or a day of time I might be able to save with this control. It took me about 30 minutes to write and test. 

The Copyright control solves a very common problem for me and that is how to keep my Copyright notices current on every page on every site? Going through by hand on January 1 of every year just is not an option. Leaving the static copyright notice just looks dumb and makes visitors think you don't care about the site. The control does a running display of the copyright notice, thus it is a set it and forget it utility control.

The control itself consist of 3 public properties, CompanyName, StartYear and FormatString. The CompanyName represents the company that owns the site or how ever you want to handle that. The start year is the first year value that should be displayed in the Copyright string. Finally I implemented a Format String property to hold a special string that will allow you to somewhat control the display. The StartYear is an important value since the site started at a particual time, a typical Copyright notice includes the first year to the current year.

The FormatString property is optional, but gives you the flexibility to control where the different values are going to be displayed. There are three values the control displays {0 = CompanyName; 1 = StartYear; 2 = CurrentYear}. So if you wanted to just show the © with the years the FormatString would look like "© {1}-{2}". So basically you control what is displayed with standard string format syntax for 3 items.

Imports System

Imports System.ComponentModel

Imports System.Drawing.Design

Imports System.Security.Permissions

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

 

 

 

Public Class Copyright

    Inherits WebControl

 

#Region " Properties "

 

    <DefaultValue("{0}"), Localizable(True)> _

    Public Overridable Property FormatString() As String

        Get

            Dim obj1 As Object = Me.ViewState.Item("FormatString")

            If (Not obj1 Is Nothing) Then

                Return CStr(obj1)

            End If

            Return String.Empty '"{0} {1}-{2}"

        End Get

        Set(ByVal value As String)

            Me.ViewState.Item("FormatString") = value

        End Set

    End Property

 

    <Themeable(False), Bindable(True), DefaultValue("2000")> _

    Public Property StartYear() As String

        Get

            Dim text1 As String = CStr(Me.ViewState.Item("StartYear"))

            If (Not text1 Is Nothing) Then

                Return text1

            End If

            Return String.Empty

        End Get

        Set(ByVal value As String)

            Me.ViewState.Item("StartYear") = HttpUtility.HtmlEncode(value)

        End Set

    End Property

 

    <Themeable(False), Bindable(True), DefaultValue("")> _

    Public Property CompanyName() As String

        Get

            Dim text1 As String = CStr(Me.ViewState.Item("CompanyName"))

            If (Not text1 Is Nothing) Then

                Return text1

            End If

            Return String.Empty

        End Get

        Set(ByVal value As String)

            Me.ViewState.Item("CompanyName") = HttpUtility.HtmlEncode(value)

        End Set

    End Property

 

#End Region

 

#Region " Method Overrides "

 

    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)

 

        Me.AddAttributesToRender(writer)

        If (Not Me.Page Is Nothing) Then

            Me.Page.VerifyRenderingInServerForm(Me)

        End If

 

        Dim text2 As String = FormatString

 

        If String.IsNullOrEmpty(StartYear) = False Then

 

            If String.IsNullOrEmpty(text2) Then

 

                If StartYear <> Now.Year.ToString Then

                    writer.Write(String.Format("Copyright © {0} {1}-{2}", _

                        CompanyName, StartYear, Now.Year.ToString))

                Else

                    writer.Write(String.Format("Copyright © {0} {1}", _

                        CompanyName, Now.Year.ToString))

                End If

 

            Else

 

                If StartYear <> Now.Year.ToString Then

                    writer.Write(String.Format(text2, _

                        CompanyName, StartYear, Now.Year.ToString))

                Else

                    text2 = text2.Remove(0, "-{2}").Remove(0, "{2}")

                    writer.Write(String.Format(text2, _

                            CompanyName, Now.Year.ToString))

                End If

 

            End If

 

        Else

            writer.Write(String.Format("Copyright © {0} {1}", _

                CompanyName, Now.Year.ToString))

        End If

    End Sub

 

#End Region

 

End Class

 



Copyright Control in a Browser.
Finally the control inherits from WebControl so we get all the plumbing for the Apperance, Behavior, etc. properties. This is really cool, so I took advantage of it!

I have included the source code for the control. All you will have to do is compile it, add it to your Visual Studio Toolbox or go about adding it to your web site by hand (but why?). Then you can drag the control onto your page, enter the values for the properties and format it to make it look like you want, save and open it in a web browser. Voila! A common task has now been simplified.

I plan on possibly adding some more properties to the control, and if so I will update things here. I have some more controls I have been working on and plan on sharing here as time warrants. If you have an idea for the control, nothing too hard or asking me to do your job please, drop me a line and let me know. I may takle the problem.
kick it on DotNetKicks.com

Posted: Thursday, February 01, 2007 3:44 PM

by Chris Love
Filed under:

Comments

Chris Love's Official Blog - Professional ASP.NET said:

I had a request for the Copyright control in C#. Of course the compiled control will work with any .NET

# February 2, 2007 1:05 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 20, 2007 1:17 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS