Click here to Skip to main content
11,717,541 members (81,579 online)
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 98.7K 2.6K 122
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.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace SubSonic
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DropDown runat=server></{0}:DropDown>")]
    public class DropDown : DropDownList
    {
        private string promptText = "--Please Select--";
        public string PromptText
        {
            get { return promptText; }
            set { promptText = value; }
        }
        private bool showPrompt;

        public bool ShowPrompt
        {
            get { return showPrompt; }
            set { showPrompt = value; }
        }

        private string promptValue = string.Empty;
        public string PromptValue
        {
            get { return promptValue; }
            set { promptValue = value; }
        }

        private string providerName = string.Empty;
        public string ProviderName
        {
            get { return providerName; }
            set { providerName = value; }
        }

        private string tableName = string.Empty;
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        private string textField = string.Empty;
        public string TextField
        {
            get { return textField; }
            set { textField = value; }
        }

        private string valueField = string.Empty;
        public string ValueField
        {
            get { return valueField; }
            set { valueField = value; }
        }

        private string orderField = string.Empty;
        public string OrderField
        {
            get { return orderField; }
            set { orderField = value; }
        }

        private string whereField = string.Empty;
        public string WhereField
        {
            get { return whereField; }
            set { whereField = value; }
        }
        private string whereValue = string.Empty;
        public string WhereValue
        {
            get { return whereValue; }
            set { whereValue = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);


            if (!DesignMode)
            {
                //load em up
                //cheap way to check for load state
                if (Items.Count == 0)
                {
                    if (!String.IsNullOrEmpty(tableName))
                    {
                        Query q = new Query(tableName, providerName);

                        if (String.IsNullOrEmpty(valueField) || String.IsNullOrEmpty(textField))
                        {
                            //look it up using the table schema
                            TableSchema.Table tbl = DataService.GetSchema(tableName, providerName, TableType.Table);
                            if (tbl != null)
                            {
                                //set the fields accordingly
                                if (String.IsNullOrEmpty(valueField))
                                {
                                    valueField = tbl.PrimaryKey.ColumnName;
                                }

                                if (String.IsNullOrEmpty(textField))
                                {
                                    if (tbl.Columns.Count > 1)
                                    {
                                        textField = tbl.Columns[1].ColumnName;
                                    }
                                    else
                                    {
                                        textField = tbl.Columns[0].ColumnName;
                                    }
                                }
                            }
                            else
                            {
                                throw new Exception("Table name '" + tableName + "' using Provider '" + providerName + "' doesn't work");
                            }
                        }

                        q.SelectList = valueField + "," + textField;
                        if (!String.IsNullOrEmpty(OrderField))
                            q.OrderBy = OrderBy.Asc(OrderField);
                        else
                            q.OrderBy = OrderBy.Asc(textField);

                        if (!String.IsNullOrEmpty(WhereField))
                            q.AddWhere(WhereField, WhereValue);

                        //fire up a reader
                        IDataReader rdr = null;
                        try
                        {
                            rdr = q.ExecuteReader();
                            while (rdr.Read())
                            {
                                ListItem item = new ListItem(rdr[1].ToString(), rdr[0].ToString());
                                Items.Add(item);
                            }
                        }
                        catch (DataException x)
                        {
                            throw new Exception("Error loading up ListItems for " + ClientID + ": " + x.Message);
                        }
                        finally
                        {
                            if (rdr != null)
                            {
                                rdr.Close();
                            }
                        }
                        ListItem prompt = new ListItem(promptText, PromptValue);
                        if (showPrompt)
                            Items.Insert(0, prompt);

                        if (!String.IsNullOrEmpty(SelectedValue))
                        {
                            foreach (ListItem item in Items)
                            {
                                if (Utilities.Utility.IsMatch(item.Value, SelectedValue))
                                {
                                    item.Selected = true;
                                    break;
                                }
                            }
                        }

                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
}

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

You may also be interested in...

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