Click here to Skip to main content
11,417,750 members (46,108 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
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 SubSonic;
using System.Data;
using SubSonic.Utilities;

namespace SubSonic
{
    public class ManyManyList : CheckBoxList
    {

        private string mapTableName = String.Empty;
        public string MapTableName
        {
            get { return mapTableName; }
            set { mapTableName = value; }
        }

        private string primaryTableName = String.Empty;
        public string PrimaryTableName
        {
            get { return primaryTableName; }
            set { primaryTableName = value; }
        }

        private string primaryKeyValue = "0";
        public string PrimaryKeyValue
        {
            get { return primaryKeyValue; }
            set { primaryKeyValue = value; }
        }

        private string foreignTableName = String.Empty;
        public string ForeignTableName
        {
            get { return foreignTableName; }
            set { foreignTableName = value; }
        }

        private string providerName = String.Empty;
        public string ProviderName
        {
            get
            {
                if (string.IsNullOrEmpty(providerName))
                {
                    providerName = DataService.Provider.Name;
                }
                return providerName;
            }
            set { providerName = value; }
        }

        private string foreignTextField = String.Empty;
        public string ForeignTextField
        {
            get { return foreignTextField; }
            set { foreignTextField = value; }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (Items.Count == 0)
            {
                //make sure the default props are set

                if (String.IsNullOrEmpty(primaryKeyValue) || String.IsNullOrEmpty(primaryTableName) || String.IsNullOrEmpty(mapTableName))
                {
                    throw new Exception("Missing a setting. Please be sure to set the PrimaryKeyValue (e.g. 'ProductID', PrimaryTableName (e.g. 'Products'), and MapTableName (e.g. Product_Category_Map)");
                }

                DataProvider provider = DataService.GetInstance(providerName);
                TableSchema.Table fkTable = DataService.GetSchema(foreignTableName, providerName, TableType.Table);
                TableSchema.Table pkTable = DataService.GetSchema(primaryTableName, providerName, TableType.Table);

                string fkPK = fkTable.PrimaryKey.ColumnName;
                string foreignTextColumn = fkTable.Columns[1].ColumnName;
                if (!string.IsNullOrEmpty(ForeignTextField))
                {
                    if (!fkTable.Columns.Contains(ForeignTextField))
                    {
                        throw new Exception("Invalid ForeignTextField. Please be sure to set the value to a field name from " + foreignTableName);
                    }
                    foreignTextColumn = ForeignTextField;
                }


                //batch this into one call
                string idParam = Utility.PrefixParameter("id", provider);
                QueryCommand cmd = new QueryCommand("SELECT " + fkPK + "," + foreignTextColumn + " FROM " + foreignTableName + ";", providerName);
                cmd.CommandSql += "SELECT " + fkPK + " FROM " + mapTableName + " WHERE " + pkTable.PrimaryKey.ColumnName + " = " + idParam;
                cmd.Parameters.Add(idParam, primaryKeyValue);

                //load the list items
                using (IDataReader rdr = DataService.GetReader(cmd))
                {
                    while (rdr.Read())
                    {
                        ListItem item = new ListItem(rdr[1].ToString(), rdr[0].ToString());
                        Items.Add(item);
                    }

                    rdr.NextResult();
                    while (rdr.Read())
                    {
                        string thisVal = rdr[fkPK].ToString().ToLower();
                        foreach (ListItem loopItem in Items)
                        {
                            if (Utility.IsMatch(loopItem.Value, thisVal))
                            {
                                loopItem.Selected = true;
                            }
                        }
                    }
                }
            }
        }

        public void Save()
        {
            QueryCommandCollection coll = new QueryCommandCollection();
            DataProvider provider = DataService.GetInstance(providerName);
            TableSchema.Table fkTable = DataService.GetSchema(foreignTableName, providerName, TableType.Table);
            TableSchema.Table pkTable = DataService.GetSchema(primaryTableName, providerName, TableType.Table);
            string fkPK = fkTable.PrimaryKey.ColumnName;
            string pk = pkTable.PrimaryKey.ColumnName;

            //delete out the existing
            string idParam = Utility.PrefixParameter("id", provider);
            QueryCommand cmdDel = new QueryCommand("DELETE FROM " + mapTableName + " WHERE " + pk + " = " + idParam, providerName);
            cmdDel.AddParameter(idParam, primaryKeyValue);
            //cmdDel.ProviderName = Product.Schema.ProviderName;

            //add this in
            coll.Add(cmdDel);

            //loop the items and insert
            string fkParam = Utility.PrefixParameter("fkID", provider);
            string pkParam = Utility.PrefixParameter("pkID", provider);

            foreach (ListItem l in Items)
            {
                if (l.Selected)
                {
                    string iSql = "INSERT INTO " + mapTableName + " (" + fkPK + ", " + pk + ")" + " VALUES (" + fkParam + "," + pkParam + ")";

                    QueryCommand cmd = new QueryCommand(iSql, providerName);
                    cmd.Parameters.Add(fkParam, l.Value);
                    cmd.Parameters.Add(pkParam, primaryKeyValue);

                    coll.Add(cmd);
                }
            }
            //execute
            DataService.ExecuteTransaction(coll);
        }
    }
}

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