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

Implementing Model-View-Presenter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
17 Nov 2007CPOL12 min read 129.8K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions