Click here to Skip to main content
15,885,915 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.5K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
using System;
using System.Data;
using System.Web.UI.WebControls;
using Model.Data.Interfaces;
using Presentation;
using Presentation.Interfaces;

/// <remarks>
///		Class Views_ProductsView is our IProductsView. It is the View in the MVP relationship. 
///		This view knows about its presenter, and it must attach the presenter. 
///		The View initializer (ASPX) creates the MVP relationship. 
/// </remarks>
public partial class Views_ProductsView : System.Web.UI.UserControl, IProductsView
{
	private ProductsPresenter presenter = null;

	#region IProductsView Members

	public void AttachPresenter(ProductsPresenter presenter)
	{
		Check.IsNotNull(presenter, "presenter cannot be null");

		this.presenter = presenter;
	}

	public void SetProducts(IDataReader productsReader)
	{
		ddlProducts.DataSource = productsReader;
		ddlProducts.DataValueField = "ProductID";
		ddlProducts.DataTextField ="ProductName";
		ddlProducts.DataBind();
		ddlProducts.Items.Insert(0, new ListItem("Select Product", "0"));
	}

	public void SetProductDetails(IProduct product)
	{
		if (product == null)
		{
			lblCategoryID.Text = string.Empty;
			lblDiscontinued.Text = string.Empty;
			lblProductID.Text = string.Empty;
			lblProductName.Text = string.Empty;
			lblQuantityPerUnit.Text = string.Empty;
			lblReorderLevel.Text = string.Empty;
			lblSupplierID.Text = string.Empty;
			lblUnitPrice.Text = string.Empty;
			lblUnitsInStock.Text = string.Empty;
			lblUnitsInOrder.Text = string.Empty;
		}
		else
		{
			lblCategoryID.Text = product.CategoryID.ToString();
			lblDiscontinued.Text = product.Discontinued.ToString();
			lblProductID.Text = product.ProductID.ToString();
			lblProductName.Text = product.ProductName;
			lblQuantityPerUnit.Text = product.QuantityPerUnit;
			lblReorderLevel.Text = product.ReorderLevel.ToString();
			lblSupplierID.Text = product.SupplierID.ToString();
			lblUnitPrice.Text = product.UnitPrice.ToString();
			lblUnitsInStock.Text = product.UnitsInStock.ToString();
			lblUnitsInOrder.Text = product.UnitsOnOrder.ToString();
		}
	}

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

	#endregion

	protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
	{
		presenter.UpdateProductDetails(ddlProducts.SelectedValue);
		if(int.Parse(ddlProducts.SelectedValue) > 0)
		{
			btnEdit.Visible = true;
		}
		else
		{
			btnEdit.Visible = false;
		}
	}
	protected void btnEdit_Click(object sender, EventArgs e)
	{
		presenter.EditProduct(int.Parse(ddlProducts.SelectedValue));
	}
}

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