VS.NET - ASP.NET - Mortgage Calculator Composite control

ASP.NET Composite Control Example.

Synopsis:

Listed below is an example of a composite asp.net web control. For this example I created a Simple mortgage payment calculator. Composite Web Controls are like Custom Web controls but rather than extending the functionality of a single control or completely designing a new one, you are simply combining several web controls. Like designing a custom control,there is no GUI support. First the composite control should inherit from a webcontrol. If it is planned to place several on one page you would need to implement the INamingContainer interface. The controls that will make up the composite control should be created in the CreateChildControls() method. Each of the controls created should then be added to the controls collection (if one control contains others only the top level control needs to be added to the Controls Collection). In the Render method the EnsureChildControls(), RenderChildren() must be called to display the control correctly on a webform in design mode. Additionaly I exposed some backcolor and border properties and one event(calculate) as examples. I have also provided a zip file of the code and dll if one would like to simply use the control as is. To use the control add it to your bin directory and add the dll directly from your toolbox using the browse button. For information on how to use a composite control: More info

Solution:

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.5 License.
Download : Dll and Code


Imports System.ComponentModel Imports System.Web.UI Namespace mCalc Public Class MCalc Inherits WebControls.WebControl Implements INamingContainer Private _backColor As System.Drawing.Color = Drawing.Color.White Private _ResultColor As System.Drawing.Color = Drawing.Color.DarkBlue Public Overrides Property BackColor() As System.Drawing.Color Get Return _backColor End Get Set(ByVal Value As Drawing.Color) EnsureChildControls() _backColor = Value DirectCast(Me.FindControl("tbl"), HtmlControls.HtmlTable).BgColor = _ "#" & Hex(_backColor.R) & Hex(_backColor.G) & Hex(_backColor.B) End Set End Property Public Overrides Property BorderColor() As System.Drawing.Color Get Return MyBase.BorderColor End Get Set(ByVal Value As Drawing.Color) EnsureChildControls() MyBase.BorderColor = Value DirectCast(Me.FindControl("pnl"), WebControls.Panel).BorderColor = _ MyBase.BorderColor End Set End Property Public Overrides Property BorderWidth() As System.Web.UI.WebControls.Unit Get Return MyBase.BorderWidth End Get Set(ByVal Value As System.Web.UI.WebControls.Unit) EnsureChildControls() MyBase.BorderWidth = Value DirectCast(Me.FindControl("pnl"), WebControls.Panel).BorderWidth = _ MyBase.BorderWidth End Set End Property Public Property ResultColor() As System.Drawing.Color Get Return _ResultColor End Get Set(ByVal Value As Drawing.Color) EnsureChildControls() _ResultColor = Value DirectCast(Me.FindControl("BasePayment"), WebControls.Label).ForeColor = _ResultColor DirectCast(Me.FindControl("TaxPayment"), WebControls.Label).ForeColor = _ResultColor DirectCast(Me.FindControl("InsurancePayment"), WebControls.Label).ForeColor = _ResultColor DirectCast(Me.FindControl("TotalPayment"), WebControls.Label).ForeColor = _ResultColor End Set End Property Public Event Calculate(ByVal Sender As Object, ByVal E As EventArgs) Protected Sub onCalculate(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim Years As String = DirectCast(Me.FindControl("Years"), WebControls.TextBox).Text Dim Rate As String = DirectCast(Me.FindControl("Rate"), WebControls.TextBox).Text Dim Loan As String = DirectCast(Me.FindControl("Loan"), WebControls.TextBox).Text Dim YearlyTax As String = DirectCast(Me.FindControl("Tax"), WebControls.TextBox).Text Dim YearlyInsurance As String = DirectCast(Me.FindControl("Insurance"), WebControls.TextBox).Text Dim BasePayment As Decimal Dim TaxPayment As Decimal Dim InsurancePayment As Decimal If IsNumeric(Years) AndAlso IsNumeric(Rate) AndAlso IsNumeric(Loan) Then BasePayment = _ (Financial.Pmt((Rate / 100) / 12, Years * 12, Loan, 0, DueDate.EndOfPeriod) * -1) DirectCast(Me.FindControl("BasePayment"), WebControls.Label).Text = _ String.Format("{0:c}", BasePayment) End If If IsNumeric(YearlyTax) Then TaxPayment = (YearlyTax / 12) DirectCast(Me.FindControl("TaxPayment"), WebControls.Label).Text = _ String.Format("{0:c}", TaxPayment) End If If IsNumeric(YearlyInsurance) Then InsurancePayment = (YearlyInsurance / 12) DirectCast(Me.FindControl("InsurancePayment"), WebControls.Label).Text = _ String.Format("{0:c}", InsurancePayment) End If DirectCast(Me.FindControl("TotalPayment"), WebControls.Label).Text = _ String.Format("{0:c}", (BasePayment + TaxPayment + InsurancePayment)) RaiseEvent Calculate(sender, e) End Sub Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter) EnsureChildControls() RenderChildren(output) End Sub Protected Overrides Sub CreateChildControls() Dim tbl As New System.Web.UI.HtmlControls.HtmlTable Dim row As New System.Web.UI.HtmlControls.HtmlTableRow Dim cell As New System.Web.UI.HtmlControls.HtmlTableCell Dim lbl As New System.Web.UI.WebControls.Label Dim txt As New System.Web.UI.WebControls.TextBox Dim pnl As New System.Web.UI.WebControls.Panel pnl.ID = "pnl" pnl.BorderColor = MyBase.BorderColor pnl.BorderWidth = MyBase.BorderWidth 'row 1 tbl.ID = "tbl" tbl.Width = "250px" tbl.BgColor = "#" & Hex(_backColor.R) & Hex(_backColor.G) & Hex(_backColor.B) tbl.Rows.Add(row) lbl.Text = "Years:" cell.Controls.Add(lbl) row.Cells.Add(cell) txt.ID = "Years" txt.Width = WebControls.Unit.Pixel(150) cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(txt) row.Cells.Add(cell) 'row 2 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Rate:" cell.Controls.Add(lbl) row.Cells.Add(cell) txt = New System.Web.UI.WebControls.TextBox txt.ID = "Rate" txt.Width = WebControls.Unit.Pixel(150) cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(txt) row.Cells.Add(cell) 'row 3 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Loan Amount:" cell.Controls.Add(lbl) row.Cells.Add(cell) txt = New System.Web.UI.WebControls.TextBox txt.ID = "Loan" txt.Width = WebControls.Unit.Pixel(150) cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(txt) row.Cells.Add(cell) 'row 4 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Yearly Tax:" cell.Controls.Add(lbl) row.Cells.Add(cell) txt = New System.Web.UI.WebControls.TextBox txt.ID = "Tax" txt.Width = WebControls.Unit.Pixel(150) cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(txt) row.Cells.Add(cell) 'row 5 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Yearly Insurance:" cell.Controls.Add(lbl) row.Cells.Add(cell) txt = New System.Web.UI.WebControls.TextBox txt.ID = "Insurance" txt.Width = WebControls.Unit.Pixel(150) cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(txt) row.Cells.Add(cell) 'Calc row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.ColSpan = 2 cell.Align = "center" tbl.Rows.Add(row) Dim calc As New Web.UI.WebControls.Button calc.Text = "Calculate" AddHandler calc.Click, AddressOf onCalculate cell.Controls.Add(calc) row.Cells.Add(cell) 'Results 'row 1 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Results" lbl.ForeColor = System.Drawing.Color.Maroon lbl.Font.Bold = True cell.Controls.Add(lbl) row.Cells.Add(cell) 'row 1 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Base Payment:" cell.Controls.Add(lbl) row.Cells.Add(cell) lbl = New System.Web.UI.WebControls.Label lbl.ID = "BasePayment" lbl.ForeColor = _ResultColor cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(lbl) row.Cells.Add(cell) 'row 2 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Tax:" cell.Controls.Add(lbl) row.Cells.Add(cell) lbl = New System.Web.UI.WebControls.Label lbl.ID = "TaxPayment" lbl.ForeColor = _ResultColor cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(lbl) row.Cells.Add(cell) 'row 3 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Insurance:" cell.Controls.Add(lbl) row.Cells.Add(cell) lbl = New System.Web.UI.WebControls.Label lbl.ID = "InsurancePayment" lbl.ForeColor = _ResultColor cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(lbl) row.Cells.Add(cell) 'row 4 row = New System.Web.UI.HtmlControls.HtmlTableRow cell = New System.Web.UI.HtmlControls.HtmlTableCell tbl.Rows.Add(row) lbl = New System.Web.UI.WebControls.Label lbl.Text = "Total:" cell.Controls.Add(lbl) row.Cells.Add(cell) lbl = New System.Web.UI.WebControls.Label lbl.ID = "TotalPayment" lbl.ForeColor = _ResultColor cell = New System.Web.UI.HtmlControls.HtmlTableCell cell.Controls.Add(lbl) row.Cells.Add(cell) pnl.Width = System.Web.UI.WebControls.Unit.Pixel(250) pnl.Controls.Add(tbl) Me.Controls.Add(pnl) End Sub End Class End Namespace
About this page: