Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Implementing Model-View-Presenter in ASP.NET

, 17 Nov 2007 CPOL
Three implementations of Model-View-Presenter in ASP.NET 2.0.
MVPSampleApp.zip
MVP.SampleApp
Lib
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
MySql.Data.dll
nunit.framework.dll
Rhino.Mocks.dll
Model
Data
Interfaces
Properties
Presentation
Presentation.Tests
Properties
Interfaces
Properties
SubSonic
ActiveRecord
Builder
CodeGeneration
Templates
CodeLanguage
Configuration
Controls
Calendar
lang
skin
active-bg.gif
calendar.gif
dark-bg.gif
hover-bg.gif
menuarrow.gif
normal-bg.gif
rowhover-bg.gif
status-bg.gif
title-bg.gif
today-bg.gif
Resources
DataProviders
Properties
Sql Tools
SubSonic.snk
Sugar
WebApp
App_Data
Views
SQL2000SampleDb.zip
SQL2000SampleDb.msi
<%@ Page Language="C#" %>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="SubSonic.Utilities"%>
<%@ Import Namespace="SubSonic" %>
<% 
	string masterPage = "#MASTERPAGE#";
	string tableName = "#TABLENAME#";
	string className = "#CLASSNAME#";
	string generatedClassName = "#PAGEFILE#";
	string fileExtension = "#LANGEXTENSION#";
	string providerName = "#PROVIDER#";
	DataProvider provider = DataService.GetInstance(providerName);
	TableSchema.Table tbl = DataService.GetSchema(tableName, provider.Name, TableType.Table);

%>

<%
	if (String.IsNullOrEmpty(masterPage))
	{
%>
[<]%@ Page Language="VB" Title="<%=className%> Scaffold" CodeFile="<%=generatedClassName%>.aspx<%=fileExtension%>" Inherits="<%=provider.GeneratedNamespace%>.<%=generatedClassName%>" %[>]
[<]!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[>]
[<]html xmlns="http://www.w3.org/1999/xhtml" [>]
[<]head id="Head1" runat="server"[>]
[<]title[>]<%=className%>[<]/title[>]
[<]script language="javascript" type="text/javascript"[>]

	function CheckDelete()
	{ 
		  return confirm('Delete this record? This action cannot be undone...'); 
	}

	function imposeMaxLength(e, Object, MaxLen, rowIndex)
	{
		var keyCode = e.keyCode;
		var counter = document.getElementById('counter' + rowIndex);
		var charText = Object.value;
		var charCount = charText.length;
		var charRemain = MaxLen - charCount;
		counter.style.visibility = 'visible';
		if(keyCode == 8 || keyCode == 46)
		{
			if(charCount == MaxLen)
			{
				charRemain = 1;
			}
			else if(charCount == 0)
			{
				charRemain = MaxLen;
			}
			counter.innerHTML = charRemain;
			return true;
		}
		else
		{
			if(charRemain [>] 0)
			{
				counter.innerHTML = charRemain;
				return true;
			}
			else
			{
				Object.value = charText.substring(0, MaxLen);
				counter.innerHTML = '0';
				return false;
			}
		}
	}
[<]/script[>]
[<]/head[>]
[<]body[>]
	[<]form id="elForm" runat="server"[>]
	
	<%
	}
	else
	{
%>
[<]%@ Page Language="VB" Title="<%=className%> Scaffold" CodeFile="<%=generatedClassName%>.aspx<%=fileExtension%>" Inherits="<%=provider.GeneratedNamespace%>.<%=generatedClassName%>" MasterPageFile="<%= masterPage %>" Theme="default" %[>]
[<]asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server"[>]
<%
	}
%>
	
	[<]h2[>]<%= className %>[<]/h2[>]
	[<]asp:Panel id="pnlGrid" runat="server"[>]
    [<]asp:GridView 
    ID="GridView1"
    runat="server" 
    AllowPaging="True" 
    AllowSorting="True"
    AutoGenerateColumns="False" 
    OnDataBound="GridView1_DataBound" 
    OnSorting="GridView1_Sorting"
	OnPageIndexChanging="GridView1_PageIndexChanging"
    DataKeyNames="<%= tbl.PrimaryKey.ColumnName %>" 
    PageSize="50"
    [>]
        [<]Columns[>]
	            [<]asp:HyperLinkField Text="Edit" DataNavigateUrlFields="<%= tbl.PrimaryKey.ColumnName %>" DataNavigateUrlFormatString="<%= generatedClassName %>.aspx?id={0}" /[>]
	            <% 
					foreach (TableSchema.TableColumn col in tbl.Columns)
					{
					    if(!col.IsPrimaryKey)
					    {
%>
	            [<]asp:BoundField DataField="<%=col.ColumnName%>" HeaderText="<%=col.DisplayName%>" SortExpression="<%=col.ColumnName%>"[>][<]/asp:BoundField[>]
	            <%
					    }
					}
%>
        [<]/Columns[>]
        [<]EmptyDataTemplate[>]
            No <%= tbl.ClassNamePlural %> 
        [<]/EmptyDataTemplate[>]
        [<]PagerTemplate[>]
        
            [<]div style="border-top:1px solid #666666"[>]
            [<]br /[>]
           [<]asp:Button ID="btnFirst" runat="server" CssClass="scaffoldButton" Text="[<][<] First" CommandArgument="First" CommandName="Page"/[>]
            [<]asp:Button ID="btnPrev" runat="server" CssClass="scaffoldButton" Text="[<] Previous" CommandArgument="Prev" CommandName="Page"/[>]

                Page
                [<]asp:DropDownList ID="ddlPages" runat="server" CssClass="scaffoldEditItem" AutoPostBack="True" OnSelectedIndexChanged="ddlPages_SelectedIndexChanged"[>]
                [<]/asp:DropDownList[>] of [<]asp:Label ID="lblPageCount" runat="server"[>][<]/asp:Label[>]

            [<]asp:Button ID="btnNext" runat="server" CssClass="scaffoldButton" Text="Next [>]" CommandArgument="Next" CommandName="Page"/[>]
            [<]asp:Button ID="btnLast" runat="server" CssClass="scaffoldButton" Text="Last [>][>]" CommandArgument="Last" CommandName="Page"/[>]
        [<]/PagerTemplate[>]
    [<]/asp:GridView[>]
	[<]a href="<%= generatedClassName %>.aspx?id=0"[>]Add New...[<]/a[>]
	[<]/asp:Panel[>]
	
	[<]asp:panel id="pnlEdit" Runat="server"[>]
	[<]asp:Label ID="lblResult" runat="server"[>][<]/asp:Label[>]	
	
	[<]table class="scaffoldEditTable" cellpadding="5" cellspacing="0" Width="600px"[>]
			[<]tr[>]
			[<]td class="scaffoldEditItemCaption"[>]<%= tbl.PrimaryKey.DisplayName %>[<]/td[>]
			[<]td class="scaffoldEditItem"[>][<]asp:Label id="lblID" runat="server" /[>][<]/td[>]
		[<]/tr[>]
	<%
		foreach (TableSchema.TableColumn col in tbl.Columns)
		{
		    if(!col.IsPrimaryKey)
		    {
%>
		[<]tr[>]
			[<]td class="scaffoldEditItemCaption"[>]<%=col.DisplayName%>[<]/td[>]
			<%
		        string controlID = "ctrl" + col.PropertyName;
		        string displayPrefix = String.Empty;
		        string controlType;
		        string controlProperties = String.Empty;

		        bool isTextBox = false;

		        if(col.IsForeignKey)
		        {
		            controlType = "DropDownList";
		        }
		        else
		        {
		            switch(col.DataType)
		            {
		                case DbType.Binary:
		                case DbType.Byte:
		                    controlType = String.Empty;
		                    break;
		                case DbType.Guid:
		                case DbType.AnsiString:
		                case DbType.String:
		                case DbType.StringFixedLength:
		                case DbType.Xml:
		                case DbType.Object:
		                case DbType.AnsiStringFixedLength:
		                    if(Utility.IsMatch(col.ColumnName, ReservedColumnName.CREATED_BY) || Utility.IsMatch(col.ColumnName, ReservedColumnName.MODIFIED_BY))
		                    {
		                        controlType = "Label";
		                    }
		                    else
		                    {
		                        isTextBox = true;
		                        controlType = "TextBox";
		                        if(Utility.GetEffectiveMaxLength(col) > 250)
		                        {
		                            controlProperties += "TextMode=\"MultiLine\" ";
		                            controlProperties += "Height=\"100px\" ";
		                            controlProperties += "Width=\"500px\" ";
		                        }
		                    }
		                    break;
		                case DbType.Date:
		                case DbType.Time:
		                case DbType.DateTime:
		                    if(Utility.IsMatch(col.ColumnName, ReservedColumnName.MODIFIED_ON) || Utility.IsMatch(col.ColumnName, ReservedColumnName.CREATED_ON))
		                    {
		                        controlType = "Label";
		                    }
		                    else
		                    {
		                        controlType = "CalendarControl";
		                    }
		                    break;
		                case DbType.Int16:
		                case DbType.Int32:
		                case DbType.UInt16:
		                case DbType.Int64:
		                case DbType.UInt32:
		                case DbType.UInt64:
		                case DbType.VarNumeric:
		                case DbType.Single:
		                case DbType.Currency:
		                case DbType.Decimal:
		                case DbType.Double:
		                    isTextBox = true;
		                    controlType = "TextBox";
		                    controlProperties += "Width=\"50px\" ";
		                    if(col.DataType == DbType.Currency)
		                    {
		                        displayPrefix = "$";
		                    }
		                    break;
		                case DbType.Boolean:
		                    bool isChecked = false;
		                    if(Utility.IsMatch(col.ColumnName, ReservedColumnName.IS_ACTIVE))
		                    {
		                        isChecked = true;
		                    }
		                    controlType = "CheckBox";
		                    controlProperties += "Checked=\"" + isChecked + "\" ";
		                    break;
		                default:
		                    isTextBox = true;
		                    controlType = "TextBox";
		                    break;
		            }
		        }
				int maxLength = Utility.GetEffectiveMaxLength(col);
				if (isTextBox && maxLength > 0)
				{
					controlProperties += "MaxLength=\"" + maxLength + "\" ";
				}


		        string controlMarkup = String.Empty;
		        if(!String.IsNullOrEmpty(controlType))
		        {
					string wrapper = "asp";
					if (controlType == "CalendarControl")
						wrapper = "subsonic";
					controlMarkup = displayPrefix + "<" + wrapper + ":" + controlType + " ID=\"" + controlID + "\" runat=\"server\" " + controlProperties + "></" + wrapper + ":" + controlType + ">";
		        }
%>
		[<]td class="scaffoldEditItem"[>]<%=controlMarkup%>[<]/td[>]
		[<]/tr[>]
		<%
		    }
		}
%>

		[<]tr[>]
			[<]td colspan="2" align="left"[>]
			[<]asp:Button id="btnSave" CssClass="scaffoldButton" runat="server" Text="Save" OnClick="btnSave_Click"[>][<]/asp:Button[>]&nbsp;
			[<]input type="button" onclick="location.href='<%= generatedClassName %>.aspx'" class="scaffoldButton" value="Return" /[>]
			[<]asp:Button id="btnDelete" CssClass="scaffoldButton" runat="server" CausesValidation="False" Text="Delete" OnClick="btnDelete_Click"[>][<]/asp:Button[>][<]/td[>]
		[<]/tr[>]
	[<]/table[>]
	[<]/asp:panel[>]

<%if(String.IsNullOrEmpty(masterPage))
{
%>
[<]/form[>]
[<]/body[>]
[<]/html[>]
<%
}
else 
{
%>
[<]/asp:Content[>]
<%
}
%>

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Alex Mueller
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Alex Mueller
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid