Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Age Calucate Web Custom Control

0.00/5 (No votes)
20 May 2008 1  
Creating Web Custom Control using ASP.NET 2.0

Download AgeCalWCC source files - 65.28 KB

Download AgeCalWCC.dll file - 5.59 KB

AgeCalWCC0.JPG

Introduction:

Web custom control is a combination of existing controls (like TextBox, Label) which is used as a single control. We can use this type of control for all the web applications as like other web controls.

Age Calculate Web Custom Control (AgeCalWCC) is developed using ASP.NET 2.0 with VB coding. AgeCalWCC is web custom control to calculate user’s age using user’s date of birth (DOB).

This AgeCalWCC module mainly concentrated on creation of the web custom control. This module gives approximate (for minimizing the code) age in No. of year(s), No. of month(s) and No. of day(s) from user’s date of birth (DOB).

I am sure; this AgeCalWCC module is more useful for intermediate level programmers for creating Web Custom Control.

AgeCalWCC I/O details:

AgeCalWCC1.JPG

Variable Naming:

I am using Camel type naming for denoting controls (eg. txtName), Pascal type naming for denoting functions & variables like string, integer etc., (eg. AgeCalFun, YearCurrent)

Controls used in AgeCalWCC:

Following web controls are used in the AgeCalWCC:

1.
TextBox - 2 Nos.
txtName - getting username
txtDOB - getting user’s DOB
2.
Label - 1 No.
lblResult - displaying the user's age or error message
3.
Button - 1 No.
btnButton - triggering the age calculation
4.
HTML Table - 3 X 5
tblAgeCalWCC - arranging the controls mentioned above.

Properties of AgeCalWCC:

1.
Name - Text property of the txtName TextBox control
2.
DOB - Text property of the txtDOB TextBox control
3.
ButtonCaption - Text property of the btnResult Button Control

Steps to Create AgeCalWCC:

  1. Open new web control library application.

    File --> New Project --> (Project Type column) Visual Basic --> windows --> (Templates column) Web Control Library --> (Name TextBox) AgeCalWCC

    See the Fig. 2

    AgeCalWCC2.JPG

  2. Rename the webcontrollibrary.vb as AgeCalWCC.vb in the solution explorer.

    See the Fig. 3

    AgeCalWCC3.JPG

  3. Remove all the code in the AgeCalWCC.vb and type the code mentioned in the CODE SECTION II
  4. Build --> Build AgeCalWCC

    See the Fig. 4

    AgeCalWCC4.JPG
  5. Now AgeCalWCC.dll is ready to use.

  6. Open New ASP.NET Web Site Application and add AgeCalWCC.dll in the Toolbox

    Right click the Toolbox --> Choose Items… --> Browse the AgeCalWCC.dd

    Now AgeCalWCC will appear in the Toolbox as web custom Control. You can use this control as like other web controls.

CODE SECTION I:

We should add the controls in the code behind page for developing the web custom control. The following code will add the TextBox control

       Dim txtName as New TextBox
       txtName.ID = “txtName”
       Controls.add(txtName)

CODE SECTION II:

Codes listed below are self-explanatory and commented between the coding part, so there is no need for more explanation for AgeCalWCC code.

'______________________________________________________________________________________________________
'
'                     Age Calculation Web Custom Control [AgeCalWCC]   
'
' This AgeCalWCC is developed by using ASP.NET 2.0 with VB Coding
'______________________________________________________________________________________________________

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


Namespace AgeCalWebCustomControls
    Public Class AgeCalWCC

        ''''''''''''''''''''''''''''''''''' Variable Declaration Section Start '''''''''''''''''''''''''''''
        Inherits Control
        Implements INamingContainer

        ' Controls used in the AgeCalWCC
        Dim txtName As New TextBox
        Dim txtDOB As New TextBox
        Dim btnResult As New Button
        Dim lblResult As New Label

        ' Properties used in the AgeCalWCC
        Private _DOB As Date = Format(Now, "yyyy-MM-dd")
        Private _Name As String
        Private _ButtonCaption As String = "Result"
        ''''''''''''''''''''''''''''''''''' Variable Declaration Section End '''''''''''''''''''''''''''''''

        '''''''''''''''''''''''''''''''''''Property Declaration Section Start '''''''''''''''''''''''''''''''
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
        Public Property DOB() As Date
            Get
                Return _DOB
            End Get
            Set(ByVal value As Date)
                _DOB = value
            End Set
        End Property
        Public Property ButtonCaption() As String
            Get
                Return _ButtonCaption
            End Get
            Set(ByVal value As String)
                _ButtonCaption = value
            End Set
        End Property
        ''''''''''''''''''''''''''''''''''' Property Declaration Section End ''''''''''''''''''''''''''''''''

        ''''''''''''''''''''''''''''''''''' CreateChildControls() Start ''''''''''''''''''''''''''''''''''''
               Protected Overrides Sub CreateChildControls()

            Dim TableString As String

            ' HTML table string
            TableString = ""
            TableString = TableString & "<table id='tblAgeCalWCC' width='100%'border='0'>"
            TableString = TableString & "<tr>"
            TableString = TableString & "<td width='25%'> </td>"
            TableString = TableString & "<td width='10%'> </td>"
            TableString = TableString & "<td width='25%'> </td>"
            TableString = TableString & "<td width='16%'> </td>"
            TableString = TableString & "<td width='24%'> </td>"
            TableString = TableString & "</tr>"

            TableString = TableString & "<tr>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "<td><font face='Verdana, Arial, Helvetica, sans-serif'>Name:</font></td>"
            TableString = TableString & "<td>"

            Controls.Add(New LiteralControl(TableString))

            ' TextBox control for User's Name
            txtName.ID = "txtName"
            txtName.Text = _Name
            Controls.Add(txtName)

            ' HTML table string
            TableString = ""
            TableString = TableString & "</td>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "</tr>"

            TableString = TableString & "<tr>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "<td><font face='Verdana, Arial, Helvetica, sans-serif'>DOB:</font></td>"
            TableString = TableString & "<td>"

            Controls.Add(New LiteralControl(TableString))

            'TextBox control for user's DOB
            txtDOB.ID = "txtDOB"
            txtDOB.Text = DOB
            Controls.Add(txtDOB)

            ' HTML table string
            TableString = ""
            TableString = TableString & "</td>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "</tr>"

            TableString = TableString & "<tr>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "<td colspan='2' align=center> "

            Controls.Add(New LiteralControl(TableString))

            'Button control for triggering the calculation
            btnResult.ID = "btnResult"
            btnResult.Text = _ButtonCaption
            Controls.Add(btnResult)

            AddHandler btnResult.Click, AddressOf Me.btnResult_Click

            'HTML table string
            TableString = ""
            TableString = TableString & "</td>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "</tr>"

            TableString = TableString & "<tr>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "<td colspan='3'> <font face='Verdana, Arial, Helvetica, sans-serif'>"

            Controls.Add(New LiteralControl(TableString))

            'Label control for displaying the result
            lblResult.ID = "lblResult"
            lblResult.Text = ""
            Controls.Add(lblResult)

            'HTML table control
            TableString = ""
            TableString = TableString & "</font></td>"
            TableString = TableString & "<td> </td>"
            TableString = TableString & "</tr>"
            TableString = TableString & "</table>"

            Controls.Add(New LiteralControl(TableString))

        End Sub
        ''''''''''''''''''''''''''''''''''' CreateChildControls() End ''''''''''''''''''''''''''''''''''''

        ''''''''''''''''''''''''''''''''''' Click Event of the btnResult Start ''''''''''''''''''''''''''''''
        '
        ' This function tring when user click the btnResult button control
        ' This function calls AgeCalFun with two input arguments 
        '
        Private Sub btnResult_Click(ByVal obj As System.Object, ByVal e As System.EventArgs)
            lblResult.Text = AgeCalFun(txtName.Text, txtDOB.Text)
        End Sub
        ''''''''''''''''''''''''''''''''''' Click Event of the btnResult End ''''''''''''''''''''''''''''''''

        ''''''''''''''''''''''''''''''''''' AgeCalFun() Start '''''''''''''''''''''''''''''''''''''''''''''''
        '
        ' This function calculates the users age (appoximate) using his/her Date Of Birth
        ' It is return the output as a string format
        '
        Private Function AgeCalFun(ByVal Name As String, ByVal DOB As String) As String
            Dim YearDOB, YearCurrent, MonthDOB, MonthCurrent, DayDOB, DayCurrent, YearCal, MonthCal, DayCal As Integer
            Dim Result As String

            If IsDate(DOB) Then
                YearDOB = Year(DOB)
                MonthDOB = Month(DOB)
                DayDOB = Day(DOB)

                YearCurrent = Year(Now)
                MonthCurrent = Month(Now)
                DayCurrent = Day(Now)

                If DayCurrent < DayDOB Then
                    DayCurrent = DayCurrent + 30
                    MonthCurrent = MonthCurrent - 1
                End If

                If MonthCurrent < MonthDOB Then
                    MonthCurrent = MonthCurrent + 12
                    YearCurrent = YearCurrent - 1
                End If

                DayCal = DayCurrent - DayDOB
                MonthCal = MonthCurrent - MonthDOB
                YearCal = YearCurrent - YearDOB

                Result = Name & " your age is " & _
                         Format(YearCal, "00") & " year(s), " & _
                         Format(MonthCal, "00") & " month(s) and " & _
                         Format(DayCal, "00") & " day(s)"

            Else
                Result = "Incorrect Date."
            End If

            Return Result
        End Function
        ''''''''''''''''''''''''''''''''''' AgeCalFun() End '''''''''''''''''''''''''''''''''''''''''''''''''
    End Class
End Namespace

Conclusion:

If you have any suggestions & questions regarding this AgeCalWCC module, please add the comments in the comments section.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here