Click here to Skip to main content
15,894,720 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 426.1K   22.2K   251  
Generates three tier code for ASP.NET.
<%
Function StartCLassBlock(AccessMod, ClssTitle, ClssCommentsTitle, ClssComments, Tab)
	StartCLassBlock = CommentType(ClssCommentsTitle, ClssComments, Tab ) &_ 
		ClassStart(AccessMod, ClssTitle, CodingLanguage, Tab) &_ 		
		OpenScope(CodingLanguage, Tab)
End Function

Function CommonEventParameters()
	CommonEventParameters = Declaration("", "sender", "System.Object", "1", CodingLanguage) & ", " & Declaration("", "e", "System.EventArgs", "1", CodingLanguage) 
End Function

Function TextMode(typeVal)
	Select Case typeVal
		Case "text", "email"
			TextMode = "SingleLine"		
		Case "password"
			TextMode = "Password"
		Case "textarea"
			TextMode = "Multiline"
	End Select
End Function

Function DText(DMode, val)
	If DMode = "1" Then
			DText = "Text='<%# DataBinder.Eval(Container.DataItem, """ & val &  """ )%" & ">'"	
	End If
End Function

Function SText(DMode, val)
	If DMode = "1" Then
			SText = "DataSource='<%# " & val & "_DataSource() " & "%" & ">'  DataTextField=""" & val & """ DataValueField=""" & val & """ SelectedIndex='<%# " & val & "_SelectedIndex( Convert.ToString(DataBinder.Eval(Container.DataItem,""" & val & """)))" & "%" & ">'"	
	End If
End Function

Function RegularExpVal(Val, RegExp, EText, Tab)
	RegularExpVal = Tab & "<asp:RegularExpressionValidator id=""Reg" & val & "Field"" ControlToValidate=""" & val & "Field"" Text=""" & EText & """ ValidationExpression=""" & RegExp & """ Display=""dynamic""  Runat=""Server"" />" & vbCrLf
End Function

Function WebControls(OldTypeVal, Val, Mlength, DMode)
	Dim TypeVal	
	TypeVal = Lcase(OldTypeVal)
	If Instr(TypeVal, "req") > 0 then		
		TypeVal = Left(TypeVal, len(TypeVal)- 3)		
	End if

	Select Case typeVal
		Case "text", "email", "password", "textarea"
			WebControls =  vbTab & vbTab & vbTab & "<asp:TextBox id=""" & val & "Field"" Maxlength=""" & Mlength & """ TextMode=""" & TextMode(typeVal) & """  " & DText(DMode, val) & " runat=""server"" CssClass=""inputText""></asp:TextBox>" & vbCrLf		
		Case "select","selectdynamic" 
			WebControls =  vbTab & vbTab & vbTab & "<asp:DropDownList id=""" & val & "Field"" " & SText(DMode, val) & " runat=""server"" CssClass=""inputText""></asp:DropDownList>" & vbCrLf	
		Case "date"
			WebControls = vbTab & vbTab & "<Site:GeneralDateTime id=""" & val & "Field"" runat=""server"" American=""True"" CssClass=""inputText""></Site:GeneralDateTime>" & vbCrLf
		Case "checkbox"
			WebControls =  vbTab & vbTab & vbTab & "<asp:CheckBox  id=""" & val & "Field"" runat=""server"" CssClass=""inputText""></asp:CheckBox>" & vbCrLf 
		Case else
			WebControls =  vbTab & vbTab & vbTab & "<asp:TextBox id=""" & val & "Field"" Maxlength=""" & mlength & """ TextMode=""SingleLine"" " & DText(DMode, val) & " runat=""server"" CssClass=""inputText""></asp:TextBox>" & vbCrLf  
	End Select

	If Instr(OldTypeVal, "req") > 0 Or TypeVal = "password" then
		WebControls =  WebControls & vbTab & vbTab & vbTab & "<asp:RequiredFieldValidator id=""Req" & val & "Field"" ControlToValidate=""" & val & "Field"" Display=""dynamic"" Text=""Required!"" Runat=""Server"" />" & vbCrLf	
	End if

	If TypeVal = "email" then
		WebControls =  WebControls & RegularExpVal(Val, "\S+@\S+\.\S{2,3}", "Invalid Email Address!", vbTab & vbTab)		
	ElseIf TypeVal = "textarea" and mlength > 0 then 
		WebControls =  WebControls & RegularExpVal(Val, ".{0," & Mlength & "}", "Can contain a maximum of " & Mlength & " characters ", vbTab & vbTab)		
	End If	
End Function

Function Separator(colspan)
	Separator =	vbTab & "<tr>" & vbCrLf &_
					vbTab & vbTab & "<td colspan='" & colspan & "' align='center' class='separatorCells'>&nbsp;</TD>" & vbCrLf &_
					vbTab & "</tr>" & vbCrLf 
End Function

Function RequestFields()
	Dim intLoop
	For intLoop = 1 to Request.Form.count
		If lcase(Trim(Request.Form(intLoop))) <> "Submit" then
			RequestFields = RequestFields & Request.Form(intLoop) & vbCrLf
		End if
	Next
End Function

'Function to select element of the  drop down menu when the asp page reloads
Function Checked( val1, val2 )
	If val1 <> "" And (Not ISNull(val1) And Not ISNull(val2)) Then
		If cSTR( val1 ) = cSTR( val2 )  Then
			Checked = " Checked "
		End If
	End If
End Function

Function WriteMode(Mode,Past)
	Select Case Mode
	Case 0
		Select Case Past
		Case 0
			WriteMode = "Add"
		Case 1
			WriteMode = "Adde"
	End Select
	Case 1
		WriteMode = "Update"
	Case 2
		WriteMode = "Delete"
	Case 3
		WriteMode = "Show"
	Case 4
		WriteMode = "Select"
	End Select
End Function

Function AppException()
	AppException = WebNamespace() & ".AppException"
End Function

Function ImportsUsing(NamespaceCollection, Tab)
	Dim NamespaceNames
	Dim NamespaceCount

	NamespaceNames = Split(NamespaceCollection, ",")
	NamespaceCount = UBound(NamespaceNames)

	For NumLoop = 0 to NamespaceCount
		ImportsUsing  = ImportsUsing & GetStatement(GetImportsUsing(NamespaceNames(NumLoop), CodingLanguage, Tab), CodingLanguage, "")  
	Next
End Function

Function TryStartBlock(Tab)
	TryStartBlock =	TryStart(CodingLanguage, Tab) &_
			OpenScope(CodingLanguage, Tab)  
End Function

Function CatchStartBlock(CExceptionName, CExceptionType, UseCExceptionName, Tab)
	CatchStartBlock = CloseScope(CodingLanguage, Tab) &_
			CatchStart(CExceptionName, CExceptionType, UseCExceptionName, CodingLanguage, Tab) &_
			OpenScope(CodingLanguage, Tab) 
End Function

Function FinallyStartBlock(Tab)
	FinallyStartBlock = CloseScope(CodingLanguage, Tab) &_
			FinallyStart(CodingLanguage, Tab) &_				
			OpenScope(CodingLanguage, Tab) 
End Function

Function FinallyEndBlock(Tab)
	FinallyEndBlock = TryEnd(CodingLanguage, Tab) & vbCrLf 			
End Function

Function ThrowException(AppExpName, LocExpName, Loc, Tab)
	ThrowException = GetStatement(GetThrow(GetNew(AppExpName & "(""An error occurred while executing the " & Loc & """, " & LocExpName & ")", CodingLanguage), CodingLanguage), CodingLanguage, Tab)
End Function

Function CatchFinal(Tab, CObjectName)
	CatchFinal = 	CatchStartBlock("ex", "System.Exception", "1", Tab & vbTab ) &_
			ThrowException(AppException() , "ex", CObjectName, Tab &  vbTab & vbTab) & vbCrLf &_
			FinallyStartBlock(Tab & vbTab) &_
			GetComment("Some action", CodingLanguage, Tab & vbTab & vbTab) &_
			FinallyEndBlock(Tab & vbTab)
End Function

Function DeclareReturnValue(Tab)
	DeclareReturnValue = GetStatement(Declaration("", "ReturnValue", "Int32", "0", CodingLanguage), CodingLanguage, Tab)
End Function

Function RequestSave()
	RequestSave = WriteTitle() & vbCrLf & vbCrLf & RequestFields()
End Function

Function WriteTitle()
	If right(Title,1) = "s" then
		WriteTitle = Title
	Else
		WriteTitle = Title & "s"
	End if
End Function

Function Fields()
	Dim NumLoop
	Fields= FieldId & ", " 
	FOR NumLoop = 0 to FieldCount
		Fields = Fields & AllFieldsNames(NumLoop) & ", "
	NEXT
End Function

Function DeclareFieldCntrl(Tab)
	Dim NumLoop
	For NumLoop = 0 to (FieldCount)
		DeclareFieldCntrl = DeclareFieldCntrl & FieldCntrl("",InputFieldsNames(NumLoop),AllFieldsNames(NumLoop), Tab ) 	 
	Next		
End Function

Function DeclareAddEditCntrl(Mode, Tab)
	Dim NumLoop
	FOR NumLoop = 0 to FieldCount	
		DeclareAddEditCntrl = DeclareAddEditCntrl & FieldCntrl("Protected", InputFieldsNames(NumLoop),AllFieldsNames(NumLoop), Tab ) 	
	NEXT
		DeclareAddEditCntrl = DeclareAddEditCntrl & GetStatement(Declaration("Protected", WriteMode(Mode, 0) & "Record", "System.Web.UI.WebControls.Button", "0", CodingLanguage), CodingLanguage, Tab) 	
End Function

Function DeclareDefaultCntrls()
	DeclareDefaultCntrls =	GetStatement(Declaration("Protected", "MsDataGrid", "System.Web.UI.WebControls.DataGrid", "0", CodingLanguage), CodingLanguage, vbTab & vbTab) &_ 
				GetStatement(Declaration("Protected", "lnk" & ObjectName, "System.Web.UI.WebControls.HyperLink", "0", CodingLanguage), CodingLanguage, vbTab & vbTab)
End Function

Function FieldCntrl(AccessModifier, typeVal,Val, Tab)
	Select Case Lcase(typeVal)
		Case "text","textreq","password","textarea"
			FieldCntrl = GetStatement(Declaration(AccessModifier, Val & "Field", "System.Web.UI.WebControls.TextBox", "0", CodingLanguage), CodingLanguage, Tab)
		Case "selectreq","selectdynamicreq","select","selectdynamic"
			FieldCntrl = GetStatement(Declaration(AccessModifier, Val & "Field", "System.Web.UI.WebControls.DropDownList", "0", CodingLanguage), CodingLanguage, Tab)		
		Case "checkbox"
			FieldCntrl = GetStatement(Declaration(AccessModifier, Val & "Field", "System.Web.UI.WebControls.CheckBox", "0", CodingLanguage), CodingLanguage, Tab)
		Case "date"
			FieldCntrl = GetStatement(Declaration(AccessModifier, Val & "Field", "WinATicket.UserControls.GeneralDateTime", "0", CodingLanguage), CodingLanguage, Tab)	
		Case else
			FieldCntrl = GetStatement(Declaration(AccessModifier, Val & "Field", "System.Web.UI.WebControls.TextBox", "0", CodingLanguage), CodingLanguage, Tab)
	End Select
End Function

Function FindFieldCntrl(Tab)
	Dim NumLoop
	For NumLoop = 0 to (FieldCount)	
		FindFieldCntrl = FindFieldCntrl & DatagridFindCntrl(InputFieldsNames(NumLoop), AllFieldsNames(NumLoop), Tab)
	Next		
End Function

Function DatagridFindCntrl(typeVal, Val, Tab)
	Select Case Lcase(typeVal)
		Case "selectreq","selectdynamicreq","select","selectdynamic"
			DatagridFindCntrl = DatagridFindCntrl & GetStatement(Val & "Field = " & GetCast("e.Item.FindControl( """ & Val & "Field"" )", "System.Web.UI.WebControls.DropDownList", CodingLanguage), CodingLanguage, Tab)	
		Case else
			DatagridFindCntrl = DatagridFindCntrl & GetStatement(Val & "Field = " & GetCast("e.Item.FindControl( """ & Val & "Field"")", "System.Web.UI.WebControls.TextBox", CodingLanguage), CodingLanguage, Tab)	
	End Select	
End Function

Function SelectFields(Tab)
	Dim NumLoop
	For NumLoop = 0 to (FieldCount)
		If Instr(Lcase(InputFieldsNames(NumLoop)),"selectdynamic") Then
			If Lcase(TypeNetFieldsNames (NumLoop)) = "int32" then
				SelectFields = SelectFields & GetStatement(AllFieldsNames(NumLoop) & " = Convert.ToInt32(Param" & ObjectName & "." & AllFieldsNames(NumLoop) & ")", CodingLanguage, Tab)				

			Else
				SelectFields = SelectFields & GetStatement(AllFieldsNames(NumLoop) & " = Convert.ToString(Param" & ObjectName & "." & AllFieldsNames(NumLoop) & ")", CodingLanguage, Tab)
			End if
			
		ElseIf Instr(Lcase(InputFieldsNames(NumLoop)),"select") Then
			SelectFields = SelectFields & GetStatement(AllFieldsNames(NumLoop) & "Field.Items.FindByValue(Convert.ToString(Param" & ObjectName & "." & AllFieldsNames(NumLoop) & ")).Selected = true", CodingLanguage, Tab)		
				
		ElseIf Instr(Lcase(InputFieldsNames(NumLoop)),"date") Then
			SelectFields = SelectFields & GetStatement(AllFieldsNames(NumLoop) & "Field.DDate = Convert.ToString(Param" & ObjectName & "." & AllFieldsNames(NumLoop) & ")", CodingLanguage, Tab)			
		Else		
			SelectFields = SelectFields & GetStatement(AllFieldsNames(NumLoop) & "Field.Text = Convert.ToString(Param" & ObjectName & "." & AllFieldsNames(NumLoop) & ")", CodingLanguage, Tab)			  	
		End If
	Next		
End Function

Function ConnIni(Tab)
	ConnIni = ConnIni & GetComment("Create Instance of Connection and Command Object", CodingLanguage, Tab & vbTab) &_
				GetStatement("MsConnString = System.Configuration.ConfigurationSettings.AppSettings[ ""connectionString"" ]", CodingLanguage, Tab & vbTab) &_
				GetStatement("MsConnection = new System.Data.SqlClient.SqlConnection( MsConnString )", CodingLanguage, Tab & vbTab) 
End Function

Function CallFunctFieldIdParam(Mode)
	Select Case Mode
	Case 0,1
		CallFunctFieldIdParam = CallFunctFieldIdParam & FieldId & ", "
	End Select
End Function

Function CreateObjectInstance(InstanceMode, Tab)
	Select Case InstanceMode
		Case 0
			CreateObjectInstance = GetComment("Create the " & ObjectName & " instance.", CodingLanguage, Tab) &_ 
					GetStatement("Param" & ObjectName & " = " & GetNew(BusinessNamespace() & "." & ObjectName & "()", CodingLanguage), CodingLanguage, Tab) 
					
		Case 1
			CreateObjectInstance = GetComment("Create the " & ObjectName & " instance.", CodingLanguage, Tab) &_	
						GetStatement("Param" & ObjectName & " = " & GetNew(BusinessNamespace() & "." & ObjectName & "(" & FieldId & ")", CodingLanguage), CodingLanguage, Tab)		
	End Select
End Function

Function DeclareClass(Tab)
	DeclareClass =  GetStatement(Declaration("", "Param" & ObjectName, BusinessNamespace() & "." & ObjectName, "0", CodingLanguage), CodingLanguage, Tab) 
End Function

Function InitialiseClass(Mode, Tab)
	Select Case Mode
	Case 0,1	
		InitialiseClass = GetStatement("Param" & ObjectName & "." & FieldId & " = " & FieldId, CodingLanguage, Tab)		
		
		FOR NumLoop = 0 to FieldCount
			If Instr(Lcase(InputFieldsNames(NumLoop)),"select") Then
				If Lcase(TypeNetFieldsNames(NumLoop)) = "int32" Then
					InitialiseClass =  InitialiseClass & GetStatement("Param" & ObjectName & "." & AllFieldsNames(NumLoop) & " = Convert.ToInt32(" & AllFieldsNames(NumLoop) & "Field.SelectedItem.Value)", CodingLanguage, Tab)
				Else
					InitialiseClass =  InitialiseClass & GetStatement("Param" & ObjectName & "." & AllFieldsNames(NumLoop) & " = " & AllFieldsNames(NumLoop) & "Field.SelectedItem.Value", CodingLanguage, Tab)		
				End If
			ElseIf Instr(Lcase(InputFieldsNames(NumLoop)),"date") Then
				InitialiseClass =  InitialiseClass & GetStatement("Param" & ObjectName & "." & AllFieldsNames(NumLoop) & " = Convert.ToDateTime(" & AllFieldsNames(NumLoop) & "Field.DDate)", CodingLanguage, Tab)
			ElseIf Instr(Lcase(InputFieldsNames(NumLoop)),"checkbox") Then
				InitialiseClass =  InitialiseClass & GetStatement("Param" & ObjectName & "." & "Common.BoolInt(" & AllFieldsNames(NumLoop) & " = " & AllFieldsNames(NumLoop) & "Field.checked)", CodingLanguage, Tab)
			Else
				If Lcase(TypeNetFieldsNames(NumLoop)) = "int32" Then
					nitialiseClass =  InitialiseClass & GetStatement("Param" & ObjectName & "." & AllFieldsNames(NumLoop) & " = Convert.ToInt32(" & AllFieldsNames(NumLoop) & "Field.Text)", CodingLanguage, Tab)					
				Else
					InitialiseClass =  InitialiseClass & GetStatement("Param" & ObjectName & "." & AllFieldsNames(NumLoop) & " = " & AllFieldsNames(NumLoop) & "Field.Text", CodingLanguage, Tab)
				End If
			End If
		Next
	Case 2
		InitialiseClass = GetStatement("Param" & ObjectName & "." & FieldId & " = " & FieldId, CodingLanguage, Tab) 
	End Select
End Function

Function CallObjFunction(mode, Tab)	
	Select Case Mode
		Case 0,1,2
			CallObjFunction = GetComment("Assign the return value.", CodingLanguage, Tab) &_
					GetStatement("ReturnValue = Param" & ObjectName & "." & WriteMode(Mode,0) & "(User.Identity.Name)", CodingLanguage, Tab) 
		Case 3
			CallObjFunction = GetStatement("MsDataView = Param" & ObjectName & ".ShowAll().Tables[0].DefaultView", CodingLanguage, Tab) 
	End Select
End Function

Function CallFunction(mode,Tab)
	CallFunction =	CreateObjectInstance(0,Tab) &_
			InitialiseClass(Mode,Tab) &_			
			CallObjFunction(mode,Tab) 
End Function

%>

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