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
using System;
using System.Web.UI.WebControls;
using Presentation;
using Presentation.Interfaces;

/// <remarks>
///		Class Views_CustomersView is our ICustomerView. It is the View in the event driven
///		MVP relationship. Since this is the view in the event driven MVP, it knows nothing 
///		about its Presenter. The View initializer (ASPX) creates the MVP relationship. 
///		The view (Views_CustomersView) only raises events when certain actions occur. The view
///		is then told what to do.
/// 
///		Do use a protected virtual method to raise each event. This is only applicable to unsealed classes, 
///		not to structs or sealed classes.
/// 
///		For each event, include a corresponding protected virtual method  that raises the event. 
///		The purpose of the method is to provide a way for a derived class to handle the event using an 
///		override. Overriding is a more flexible, faster, and a more natural way to handle base class events 
///		in derived classes. The name of the method is should start with �On� and be followed with the name of the event.
/// </remarks>
public partial class Views_CustomersView : System.Web.UI.UserControl, ICustomersView
{
	protected override void OnLoad(EventArgs e)
	{
		EventHandler<SingleValueEventArgs<bool>> eventHandler = OnViewLoad;
		if (eventHandler != null)
		{
			// Invoke our delegate
			eventHandler(this, new SingleValueEventArgs<bool>(Page.IsPostBack));
		}

		base.OnLoad(e);
	}

	#region ICustomersView Members

	public event EventHandler<SingleValueEventArgs<bool>> OnViewLoad;
	public event EventHandler<SingleValueEventArgs<string>> RequestCustomerDetails;
	public event EventHandler<SingleValueEventArgs<string>> RequestCustomerEdit;
	
	public void SetCustomers(System.Data.IDataReader customerReader)
	{
		ddlCustomers.DataSource = customerReader;
		ddlCustomers.DataValueField = "CustomerID";
		ddlCustomers.DataTextField = "ContactName";
		ddlCustomers.DataBind();
		ddlCustomers.Items.Insert(0, new ListItem("Select Customers", "0"));
	}

	public void SetCustomerDetails(Model.Data.Interfaces.ICustomer customer)
	{
		if(customer == null)
		{
			lblAddress.Text = string.Empty;
			lblCity.Text = string.Empty;
			lblCompanyName.Text = string.Empty;
			lblContactName.Text = string.Empty;
			lblContactTitle.Text = string.Empty;
			lblCountry.Text = string.Empty;
			lblCustomerID.Text = string.Empty;
			lblFax.Text = string.Empty;
			lblPhone.Text = string.Empty;
			lblPostalCode.Text = string.Empty;
			lblRegion.Text = string.Empty;
		}
		else
		{
			lblAddress.Text = customer.Address;
			lblCity.Text = customer.City;
			lblCompanyName.Text = customer.CompanyName;
			lblContactName.Text = customer.ContactName;
			lblContactTitle.Text = customer.ContactTitle;
			lblCountry.Text = customer.Country;
			lblCustomerID.Text = customer.CustomerID.ToString();
			lblFax.Text = customer.Fax;
			lblPhone.Text = customer.Phone;
			lblPostalCode.Text = customer.PostalCode;
			lblRegion.Text = customer.Region;
		}
	}

	public void SetRefreshTime(DateTime dateTime)
	{
		lblTime.Text = dateTime.ToString();
	}

	#endregion

	protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
	{
		// Raise our event
		OnRequestCustomerDetails(GetSelectedCustomerID());
	}

	protected void btnEdit_Click(object sender, EventArgs e)
	{
		// Raise our event
		OnRequestCustomerEdit(GetSelectedCustomerID());
	}

	/// <summary>
	///		Raise the RequestCustomerDetails event by invoking
	///		the delegates.
	/// </summary>
	/// <param name="customerID">string customerID</param>
	public virtual void OnRequestCustomerDetails(string customerID)
	{
		EventHandler<SingleValueEventArgs<string>> eventHandler = RequestCustomerDetails;
		if (eventHandler != null)
		{
			eventHandler(this, GetCustomerEventArgs(customerID));
		}
	}

	/// <summary>
	///		Raise the RequestCustomerEdit event by invoking
	///		the delegates.
	/// </summary>
	/// <param name="customerID">string customerID</param>
	public virtual void OnRequestCustomerEdit(string customerID)
	{
		EventHandler<SingleValueEventArgs<string>> eventHandler = RequestCustomerEdit;
		if (eventHandler != null)
		{
			eventHandler(this, GetCustomerEventArgs(customerID));
		}
	}

	private string GetSelectedCustomerID()
	{
		return ddlCustomers.SelectedValue;
	}

	private SingleValueEventArgs<string> GetCustomerEventArgs(string customerID)
	{
		return new SingleValueEventArgs<string>(customerID);
	}
}

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.141223.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Alex Mueller
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid