Download AgeCalWCC source files - 65.28 KB
Download AgeCalWCC.dll file - 5.59 KB
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:
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:
-
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
-
Rename the webcontrollibrary.vb as AgeCalWCC.vb in the solution explorer.
See the Fig. 3
- Remove all the code in the AgeCalWCC.vb and type the code mentioned in the CODE SECTION II
-
Build --> Build AgeCalWCC
See the Fig. 4
-
Now AgeCalWCC.dll is ready to use.
-
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.
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace AgeCalWebCustomControls
Public Class AgeCalWCC
Inherits Control
Implements INamingContainer
Dim txtName As New TextBox
Dim txtDOB As New TextBox
Dim btnResult As New Button
Dim lblResult As New Label
Private _DOB As Date = Format(Now, "yyyy-MM-dd")
Private _Name As String
Private _ButtonCaption As String = "Result"
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
Protected Overrides Sub CreateChildControls()
Dim TableString As 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))
txtName.ID = "txtName"
txtName.Text = _Name
Controls.Add(txtName)
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))
txtDOB.ID = "txtDOB"
txtDOB.Text = DOB
Controls.Add(txtDOB)
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))
btnResult.ID = "btnResult"
btnResult.Text = _ButtonCaption
Controls.Add(btnResult)
AddHandler btnResult.Click, AddressOf Me.btnResult_Click
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))
lblResult.ID = "lblResult"
lblResult.Text = ""
Controls.Add(lblResult)
TableString = ""
TableString = TableString & "</font></td>"
TableString = TableString & "<td> </td>"
TableString = TableString & "</tr>"
TableString = TableString & "</table>"
Controls.Add(New LiteralControl(TableString))
End Sub
Private Sub btnResult_Click(ByVal obj As System.Object, ByVal e As System.EventArgs)
lblResult.Text = AgeCalFun(txtName.Text, txtDOB.Text)
End Sub
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
End Class
End Namespace
Conclusion:
If you have any suggestions & questions regarding this AgeCalWCC module, please add the comments in the comments section.