Click here to Skip to main content
11,494,174 members (60,890 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 97.1K 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.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SubSonic
{
	/// <summary>
	/// Summary description for CalendarControl.
	/// </summary>
	public class CalendarControl : TextBox
	{
		private const string DEFAULT_INVALID_DATE = "Please enter a valid date.";

		private const string DEFAULT_FORMAT = "MM/dd/yyyy";
		private const string DEFAULT_JAVASCRIPT_FORMAT = "%m/%d/%Y";
		private const string DEFAULT_LANGUAGE = "en";
		private Image imgCalendar;
		private DateTime? selectedDate;
		private string displayFormat;
		private string javaScriptFormat;
		private string language;
		private bool showTime = true;

        public CalendarControl()
		{
			//DisplayFormat = DEFAULT_FORMAT;
			//JavaScriptFormat = DEFAULT_JAVASCRIPT_FORMAT;
			//Language = DEFAULT_LANGUAGE;
            DisplayFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
            if (!String.IsNullOrEmpty(DisplayFormat))
            {
                char splitter = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator[0];
                string[] format = DisplayFormat.Split(new char[] {splitter});
                StringBuilder sbFormat = new StringBuilder();
                for(int i = 0; i<format.Length; i++)
                {
                    sbFormat.Append("%");
                    sbFormat.Append(format[i][0]);
                    if(i + 1 != format.Length)
                    {
                        sbFormat.Append(splitter);
                    }
                }
                JavaScriptFormat = sbFormat.ToString().ToLower();
            }
            else
            {
                DisplayFormat = DEFAULT_FORMAT;
                JavaScriptFormat = DEFAULT_JAVASCRIPT_FORMAT;
            }
            Language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
		}

		private Image CalendarImage
		{
			get
			{
				if (imgCalendar == null)
				{
					imgCalendar = new Image();

					imgCalendar.ID = "imgCalendar" + ClientID;
					imgCalendar.ImageAlign = ImageAlign.AbsMiddle;
				    imgCalendar.ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.skin.calendar.gif");
                        //StylePath + "calendar.gif";
					imgCalendar.Style.Add("margin-left", "5px");
				}
				return imgCalendar;
			}
		} 

		[DefaultValue(DEFAULT_FORMAT)]
		public string DisplayFormat
		{
			get { return displayFormat; }
			set { displayFormat = value; }
		} 
        
		[DefaultValue(DEFAULT_JAVASCRIPT_FORMAT)]
		public string JavaScriptFormat
		{
			get { return javaScriptFormat; }
			set { javaScriptFormat = value; }
		}

        [DefaultValue(DEFAULT_LANGUAGE)]
        public string Language
        {
            get { return language; }
            set { language = value; }
        }

        public bool ShowTime
		{
			get { return showTime; }
			set { showTime = value; }
		}

        public DateTime? SelectedDate
        {
            get
            {
                string selDate = Text.Trim();
                if (!String.IsNullOrEmpty(selDate))
                {
                    selectedDate = DateTime.Parse(selDate);
                }
                else
                {
                    selectedDate = null;
                }
                return selectedDate;
            }
            set
            {
                selectedDate = value;
                if (selectedDate.HasValue)
                {
                    DateTime dt = selectedDate.Value;
                    Text = dt.ToString(DisplayFormat);
                }
                else
                {
                    Text = String.Empty;
                }
            }
        } 

        protected override void OnPreRender(EventArgs e)
        {
            string csslink = "<link href='" + Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.skin.theme.css") + "' rel='stylesheet' type='text/css' />";
            Page.Header.Controls.Add(new LiteralControl(csslink));
            Page.ClientScript.RegisterClientScriptInclude("CalendarMain", Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.calendar.js"));
            Page.ClientScript.RegisterClientScriptInclude("CalendarSetup", Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.calendar-setup.js"));

            const string langPrefix = "SubSonic.Controls.Calendar.lang.calendar-";
            
            

            if(String.IsNullOrEmpty(Language))
            {
                Language = DEFAULT_LANGUAGE;
            }

            if (Assembly.GetExecutingAssembly().GetManifestResourceStream(langPrefix + Language + ".js") == null)
            {
                Page.ClientScript.RegisterClientScriptInclude("CalendarLanguage", Page.ClientScript.GetWebResourceUrl(GetType(), langPrefix + DEFAULT_LANGUAGE + ".js"));
            }
            else
            {
                Page.ClientScript.RegisterClientScriptInclude("CalendarLanguage", Page.ClientScript.GetWebResourceUrl(GetType(), langPrefix + Language + ".js"));
            }
            base.OnPreRender(e);
        }

	    protected override void Render(HtmlTextWriter writer)
		{
			if (SelectedDate == DateTime.MinValue)
			{
				SelectedDate = DateTime.Now;
			}

			writer.WriteLine("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
			writer.WriteLine("<tr>");
			writer.WriteLine("<td>");

			base.Render (writer);

			writer.WriteLine("</td><td>");

			CalendarImage.RenderControl(writer);  // render CalendarButton object

			writer.WriteLine("</td>");
			writer.WriteLine("</tr>");
			writer.WriteLine("</table>");

			Page.ClientScript.RegisterStartupScript(typeof(Page), "Calendar" + ClientID,
				"<script type=\"text/javascript\">" +
					"Calendar.setup( { " +
						"inputField: \"" + ClientID + "\", " +
						"ifFormat: \"" + JavaScriptFormat + "\", " +
						"button: \"" + CalendarImage.ClientID + "\", " +
						"date: \"" + SelectedDate + "\", " +
						"showsTime: " + (ShowTime ? "true" : "false") + " " +
					"} );" +
				"</script>");
		}
	}
}

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