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
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
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.Text.RegularExpressions;

namespace SubSonic {
    [ParseChildren(false)]
    [ToolboxData("<{0}:EditBox runat=server></{0}:EditBox>")]
    public class EditBox : WebControl,INamingContainer {
        
        private string editRole="Content Editor";
        public string EditRole {
            get { return editRole; }
            set { editRole = value; }
        }
	

        Panel pnlDisplay = new Panel();
        Panel pnlEdit = new Panel();
        Button btnToggle = new Button();
        Button btnSave = new Button();
        Button btnCancel = new Button();
        LinkButton lnkEdit = new LinkButton();

        TextBox tbEdit = new TextBox();
        Literal litDisplay = new Literal();


        protected override void CreateChildControls() {
            base.CreateChildControls();

            btnSave.Click += new EventHandler(btnSave_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
            lnkEdit.Click += new EventHandler(lnkEdit_Click);


            tbEdit.TextMode = TextBoxMode.MultiLine;
            tbEdit.Height = Unit.Pixel(400);
            tbEdit.Width = Unit.Pixel(300);
            
            if (!Page.IsPostBack) {
                tbEdit.Visible = false;
                ToggleEditMode(false);
            }

            btnSave.Text = "Save";
            btnCancel.Text = "Cancel";
            lnkEdit.Text = "Edit";


            this.Controls.Add(tbEdit);
            this.Controls.Add(litDisplay);
            this.Controls.Add(btnSave);
            this.Controls.Add(btnCancel);
            this.Controls.Add(lnkEdit);

        }
        void ToggleEditMode(bool showEditor) {
            
            //editor bits
            tbEdit.Visible = showEditor;
            btnSave.Visible = showEditor;
            btnCancel.Visible = showEditor;
            litDisplay.Visible = !showEditor;

            //if this user can edit, show the link
            //lnkEdit.Visible = Page.User.IsInRole(this.editRole);
        }
        void lnkEdit_Click(object sender, EventArgs e) {
            ToggleEditMode(true);
        }

        void btnCancel_Click(object sender, EventArgs e) {
            ToggleEditMode(false);
        }

        void btnSave_Click(object sender, EventArgs e) {

            //find the control, and store the text on the page, between the control tags
            //this can be tricky...
            string pageFile = Page.Request.CurrentExecutionFilePath;
            string pagePath = Page.Server.MapPath(pageFile);

            //now, find the tag in the page's text
            string regPattern = @"(?<="+this.ID+".*?>).*?(?=</)";

            //run a replace
            string pageText = Utilities.Utility.GetFileText(pagePath);

            Regex reg = new Regex(regPattern);
            pageText=reg.Replace(pageText, tbEdit.Text);

            //put the pageText back
            Utilities.Utility.WriteToFile(pagePath, pageText);

            //redirect to the page
            Page.Response.Redirect(Page.Request.CurrentExecutionFilePath);


        }
    }
}

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

You may also be interested in...

About the Author

Alex Mueller
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Alex Mueller
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid