Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

Three Tier Code Generator For ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.78/5 (34 votes)
8 Jul 200512 min read 425K   22.2K   251  
Generates three tier code for ASP.NET.
'**==========================================================================================
'**
'** FILE  : Default.aspx.vb
'** Author: Stevan Rodrigues
'**
'**==========================================================================================
'**
'** (c) The contents of this file , and of any file or document derived from it , are copyright 
'** to Webchamps . Unlicensed alteration, change or copying in any form, 
'** whether written, by photocopy, by print or by any other methods of reproduction is 
'** strictly prohibited.
'**
'**==========================================================================================
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports WebChamps.Components.Web
Imports  Company.ThreeTierDemoVB.Components.Business

'*********************************************************************
'
' Company.ThreeTierDemoVB.Admin Namespace
'
'*********************************************************************
Namespace Company.ThreeTierDemoVB.Admin
	Public Class DefaultAdmin
	Inherits WebChamps.Components.Web.SitePage
		Protected PageMessage As System.Web.UI.WebControls.Label
		Protected ErrorMessage As System.Web.UI.WebControls.Label

        Protected WithEvents MsDataGrid As System.Web.UI.WebControls.DataGrid
        Protected WithEvents NewLink As System.Web.UI.WebControls.HyperLink
        Protected lnkAdmin As System.Web.UI.WebControls.HyperLink

		#Region " Web Form Designer Generated Code "

			'This call is required by the Web Form Designer.
			<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

			Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
				'CODEGEN: This method call is required by the Web Form Designer
				'Do not modify it using the code editor.
				InitializeComponent()
			End Sub

		#End Region

		'*********************************************************************
		'
		' Page_Load event
		'
 		'The ViewState["SortExpression"] used to sort the datagrid is initialised.
 		'The datagrid is then populated using the BindDataGrid function.
		'
		'*********************************************************************
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Insert code to process code when the page is loaded for first time or without a post back.
            If Not IsPostBack Then
                If ViewState("SortExpression") <> Nothing Then
                    ViewState("SortExpression") = "AdminId"
                End If
                BindDataGrid(Convert.ToString(ViewState("SortExpression")))
            End If
        End Sub

        '*********************************************************************
        '
        ' BindDataGrid method
        '
        'The Company.ThreeTierDemoVB.Components.Data.Admin.ShowAdmin is used to populate the datagrid.
        '
        '*********************************************************************
        Private Sub BindDataGrid(ByVal SortField As String)
            Dim MsDataView As System.Data.DataView
            Dim ParamAdmin As Company.ThreeTierDemoVB.Components.Business.Admin

            'Create the Admin instance.
            ParamAdmin = New Company.ThreeTierDemoVB.Components.Business.Admin
            MsDataView = ParamAdmin.ShowAll().Tables(0).DefaultView
            MsDataView.Sort = SortField
            MsDataGrid.DataSource = MsDataView
            MsDataGrid.DataBind()
        End Sub

        '*********************************************************************
        '
        ' MsDataGrid_ItemDataBound event
        '
        'It adds client side JavaScript event to the delete BoundColumn.
        '
        '*********************************************************************
        Private Sub MsDataGrid_ItemDataBound(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MsDataGrid.ItemDataBound
            Dim DeleteButton As System.Web.UI.WebControls.LinkButton
            If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
                DeleteButton = CType(e.Item.FindControl("DeleteButton"), System.Web.UI.WebControls.LinkButton)
                DeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this company?');")
            End If
        End Sub

        '*********************************************************************
        '
        ' MsDataGrid_SortCommand event
        '
        'The ViewState["SortExpression"] is Assigned the sort expression value.
        'The datagrid is then populated using the BindDataGrid function.
        '
        '*********************************************************************
        Private Sub MsDataGrid_SortCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles MsDataGrid.SortCommand
            ViewState("SortExpression") = e.SortExpression
            BindDataGrid(Convert.ToString(ViewState("SortExpression")))
        End Sub

        '*********************************************************************
        '
        ' MsDataGrid_PageIndexChanged event
        '
        'The CurrentPageIndex is Assigned the page index value.
        'The datagrid is then populated using the BindDataGrid function.
        '
        '*********************************************************************
        Private Sub MsDataGrid_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles MsDataGrid.PageIndexChanged
            MsDataGrid.CurrentPageIndex = e.NewPageIndex
            BindDataGrid(Convert.ToString(ViewState("SortExpression")))
        End Sub

        '*********************************************************************
        '
        ' MsDataGrid_DeleteCommand event
        '
        'The record is deleted.
        'The datagrid is then populated using the BindDataGrid function.
        '
        '*********************************************************************
        Private Sub MsDataGrid_DeleteCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MsDataGrid.DeleteCommand
            Dim AdminId As Int32
            AdminId = 0
            Dim ReturnValue As Int32
            If MsDataGrid.EditItemIndex = e.Item.ItemIndex Then
                ErrorMessage.Text = "WARNING: Unable to delete record in edit mode!"
                MsDataGrid.EditItemIndex = -1
                BindDataGrid(Convert.ToString(ViewState("SortExpression")))
                Return
            End If
            PageMessage.Text = ""
            ErrorMessage.Text = ""
            AdminId = Convert.ToInt32(MsDataGrid.DataKeys(e.Item.ItemIndex))
            ReturnValue = Company.ThreeTierDemoVB.Components.Data.Admin.DeleteAdmin(AdminId, User.Identity.Name)
            If ReturnValue = 0 Then
                Dim LastPageItems As Int32

                LastPageItems = MsDataGrid.Items.Count Mod MsDataGrid.PageSize
                If LastPageItems = 1 And MsDataGrid.PageCount > 1 Then
                    MsDataGrid.CurrentPageIndex = MsDataGrid.PageCount - 2
                Else
                    MsDataGrid.CurrentPageIndex = 0
                End If

                ErrorMessage.Text = "Record deleted!"
            ElseIf ReturnValue = -1 Then
                ErrorMessage.Text = "Custom error!"
            Else
                PageMessage.Text = "Record does not exist!"
            End If

            BindDataGrid(Convert.ToString(ViewState("SortExpression")))
        End Sub

        '*********************************************************************
        '
        ' MsDataGrid_EditCommand event
        '
        'The datagrid record to be edited is selected.
        'The datagrid is then populated using the BindDataGrid function.
        '
        '*********************************************************************
        Private Sub MsDataGrid_EditCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MsDataGrid.EditCommand
            PageMessage.Text = ""
            ErrorMessage.Text = ""
            MsDataGrid.EditItemIndex = e.Item.ItemIndex
            BindDataGrid(Convert.ToString(ViewState("SortExpression")))
        End Sub

        '*********************************************************************
        '
        ' MsDataGrid_CancelCommand event
        '
        'The record to be edited is canceled.
        'The datagrid is then populated using the BindDataGrid function.
        '
        '*********************************************************************
        Private Sub MsDataGrid_CancelCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MsDataGrid.CancelCommand
            PageMessage.Text = ""
            ErrorMessage.Text = ""
            MsDataGrid.EditItemIndex = -1
            BindDataGrid(Convert.ToString(ViewState("SortExpression")))
        End Sub

        '*********************************************************************
        '
        ' MsDataGrid_UpdateCommand event
        '
        'The Company.ThreeTierDemoVB.Components.Data.Admin.updateAdmin is used to update the record.
        'The datagrid is then populated using the BindDataGrid function.
        '
        '*********************************************************************
        Private Sub MsDataGrid_UpdateCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MsDataGrid.UpdateCommand
            Dim FirstNameField As System.Web.UI.WebControls.TextBox
            Dim LastNameField As System.Web.UI.WebControls.TextBox
            Dim UsernameField As System.Web.UI.WebControls.TextBox
            Dim PasswordField As System.Web.UI.WebControls.TextBox
            Dim ReturnValue As Int32
            Dim AdminId As Int32
            AdminId = 0
            Dim ParamAdmin As Company.ThreeTierDemoVB.Components.Business.Admin

            FirstNameField = CType(e.Item.FindControl("FirstNameField"), System.Web.UI.WebControls.TextBox)
            LastNameField = CType(e.Item.FindControl("LastNameField"), System.Web.UI.WebControls.TextBox)
            UsernameField = CType(e.Item.FindControl("UsernameField"), System.Web.UI.WebControls.TextBox)
            PasswordField = CType(e.Item.FindControl("PasswordField"), System.Web.UI.WebControls.TextBox)

            AdminId = Convert.ToInt32(MsDataGrid.DataKeys(e.Item.ItemIndex))
            'Create the Admin instance.
            ParamAdmin = New Company.ThreeTierDemoVB.Components.Business.Admin
            ParamAdmin.AdminId = AdminId
            ParamAdmin.FirstName = FirstNameField.Text
            ParamAdmin.LastName = LastNameField.Text
            ParamAdmin.Username = UsernameField.Text
            ParamAdmin.Password = PasswordField.Text
            'Assign the return value.
            ReturnValue = ParamAdmin.Update(User.Identity.Name)
            If ReturnValue = 0 Then
                ErrorMessage.Text = "Record updated!"
            ElseIf ReturnValue = -1 Then
                ErrorMessage.Text = "Custom error!"
            Else
                PageMessage.Text = "Record already exists!"
            End If
            MsDataGrid.EditItemIndex = -1
            BindDataGrid(Convert.ToString(ViewState("SortExpression")))
        End Sub

    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Software Developer (Senior)
Australia Australia
Stevan is a Microsoft Certified Solutions Developer in .Net Architecture (MCSD.Net Early Achiever – one among the first 2500 worldwide), Microsoft Certified Application Developer in .Net – MCAD.Net (Charter Member - one among the first 5000 developers worldwide).

Comments and Discussions