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

ASP.NET User Controls - Notify One Control Of Changes In Other Control

Rate me:
Please Sign up or sign in to vote.
4.39/5 (23 votes)
12 Mar 20026 min read 195.5K   2.6K   87  
A tutorial on how to use delegate model to notify one ASP.NET user control of changes in the other user control.
using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using ASPNet_App.Controls;

namespace ASPNet_App
{
	/// <summary>
	/// Summary description for BannerAds.
	/// </summary>
	public class BannerAds : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Panel BannerDiv;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			//if (!IsPostBack)
			{
				// Load the UserControls into BannerAd div.
				Control bannerControl;
				Control banner;
				try
				{
					bannerControl = LoadControl("Controls/SiteBannerManager.ascx");
					banner = LoadControl("Controls/SiteBanner.ascx");
					if (bannerControl != null)
					{
						((SiteBannerManager)bannerControl).BannerChange += new BannerChangeEventHandler(((SiteBanner)banner).ChangeBannerImage);
					}

					CreateBannerControlTable(bannerControl, banner);
				}
				catch (Exception ex)
				{
					Trace.Write(ex.Message);
				}
			}
		}

		private void CreateBannerControlTable(Control leftCtl, Control rightCtl)
		{
			Table tbl = new Table();
			TableRow row = new TableRow();

			TableCell leftCol = new TableCell();
			TableCell rightCol = new TableCell();

			leftCol.Controls.Add(leftCtl);
			leftCol.HorizontalAlign = HorizontalAlign.Center;
			leftCol.VerticalAlign = VerticalAlign.Top;
			leftCol.Width = 200;

			rightCol.Controls.Add(rightCtl);
			rightCol.HorizontalAlign = HorizontalAlign.Center;
			rightCol.VerticalAlign = VerticalAlign.Top;

			row.Cells.Add(leftCol);
			row.Cells.Add(rightCol);

			tbl.Rows.Add(row);

			BannerDiv.Controls.Add(tbl);
		}
		

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			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
	}
}

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
Web Developer
United States United States
To learn more about us, Please visit us at http://www.netomatix.com

Comments and Discussions