Click here to Skip to main content
15,895,084 members
Articles / Web Development / ASP.NET

Declarative Programming--Unifying Form And Web Development

,
Rate me:
Please Sign up or sign in to vote.
4.69/5 (14 votes)
23 Nov 20044 min read 71.7K   912   39  
Use declarative programming to create UI's common for both Web and Form applets.
// Copyright (c) 2004 Marc Clifton.  All Rights Reserved

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;

using MyXaml;

namespace declarativeWeb
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Panel formPanel;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				SimpleCalc sc=new SimpleCalc();
				XmlDocument doc=LoadDocument();

				if (doc != null)
				{
					Parser parser=new Parser();
					Panel panel=(Panel)parser.LoadObject(doc, "Calc", sc, null);
					if (panel != null)
					{
						formPanel.Controls.Add(panel);
					}
				}

				Session["SimpleCalc"]=sc;
			}
			else
			{
				SimpleCalc sc=(SimpleCalc)Session["SimpleCalc"];
				XmlDocument doc=LoadDocument();

				if (doc != null)
				{
					Parser parser=new Parser();
					Panel panel=(Panel)parser.LoadObject(doc, "Calc", sc, null);
					if (panel != null)
					{
						formPanel.Controls.Add(panel);
					}
				}
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private XmlDocument LoadDocument()
		{
			XmlDocument doc=null;
			string path=MapPath("");
			
			try
			{
				doc=new XmlDataDocument();
				doc.PreserveWhitespace=true;
				doc.Load(path+"\\calcForm.xml");
				XslTransform xt=new XslTransform();
				xt.Load(path+"\\form2web.xslt");
				StringBuilder sb=new StringBuilder();
				StringWriter sw=new StringWriter(sb);
				xt.Transform(doc, null, new XmlTextWriter(sw), new XmlUrlResolver());

				doc=new XmlDocument();
				doc.LoadXml(sb.ToString());
				System.Diagnostics.Trace.WriteLine(sb.ToString());

			}
			catch(Exception ex)
			{
				Trace.Warn(ex.Message);
			}
			return doc;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Written By
Web Developer
United States United States
My main goal as a developer is to improve the way software is designed, and how it interacts with the user. I like designing software best, but I also like coding and documentation. I especially like to work with user interfaces and graphics.

I have extensive knowledge of the .NET Framework, and like to delve into its internals. I specialize in working with VG.net and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.

Comments and Discussions