Click here to Skip to main content
12,406,528 members (38,704 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

1.2M views
11.7K downloads
303 bookmarked
Posted

ASP.NET Page Templates - Using Inheritance

, 19 Sep 2002
A tutorial showing how to create page templates that use object-oriented inheritance.
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace PageInheritanceSample
{
	/// <summary>
	/// Summary description for AdvancedPageInheritanceBase.
	/// </summary>
	public class AdvancedPageInheritanceBase : PageBase
	{
		protected TextBox searchBox;

		protected override void OnInit(System.EventArgs e)
		{
			BuildPage( GenerateHtmlForm() );
			base.OnInit(e);
		}

		protected virtual void BuildPage( HtmlForm form )
		{
			////////////////////////////////////////////////////////
			// Build the page and include the generated form

			this.Controls.AddAt( 0, new LiteralControl( @"
				<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
				<html>
					<head>
						<title>" + PageTitle + @"</title>
					</head>
					<body>
						<table bgcolor='silver' width='100%'>
						<tr>
							<td colspan=2><h1>Our Page Header</h1></td>
						</tr>
						<tr>
							<td>
								Menu:
								<ul>
									<li><a href='Page1.aspx'>Page 1</a></li>
									<li><a href='Page2.aspx'>Page 2</a></li>
								</ul>
							</td>
							<td bgcolor='white'>
				") );

			this.Controls.Add( form );

			this.Controls.Add( new LiteralControl( @"
							</td>							
						</tr>
						<tr>
							<td colspan=2><small>This is our footer</small></td>
						</tr>
						</table>	
					</body>
				</html>
			"));
		}

		protected virtual HtmlForm GenerateHtmlForm()
		{
			HtmlForm form = new HtmlForm();
			AddSearch(form);
			AddControlsFromDerivedPage(form);
			return form;
		}

		protected void AddSearch( HtmlForm form )
		{
			searchBox = new TextBox();
			Button searchButton = new Button();
			searchButton.Text = "Search";
			searchButton.Click += new EventHandler( this.OnSearchButtonClicked );
			form.Controls.Add( searchBox );
			form.Controls.Add( searchButton );
			form.Controls.Add( new LiteralControl("<br>") );
		}

		protected void OnSearchButtonClicked( object sender, EventArgs e )
		{
			string searchText = searchBox.Text;
		}

		protected virtual void AddControlsFromDerivedPage(HtmlForm form)
		{
			int count = this.Controls.Count;
			for( int i = 0; i<count; ++i )
			{
				System.Web.UI.Control ctrl  = this.Controls[0];
				form.Controls.Add( ctrl );
				this.Controls.Remove( ctrl );
			}
		}
	}
}

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

Share

About the Author

Peter Provost
Web Developer
United States United States
Peter Provost has been programming computers since he was 10 years old. Currently a C# and C++ junkie, he has been developing on the Windows platform since Windows 286.

Peter is a Software Design Engineer with Microsoft's patterns and practices team where he works on guidance for .NET developers.

Peter maintains an active weblog about technology, .NET and other interesting stuff at http://www.peterprovost.org

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 20 Sep 2002
Article Copyright 2002 by Peter Provost
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid