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

How to invoke events across User Controls in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.29/5 (11 votes)
11 Sep 2006CPOL2 min read 84.2K   791   44  
How to use self-registering objects and Context.Items to invoke events across User Controls.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EventsAcrossControls
{
	public class ReceiverControl : UserControl
	{
		protected Literal uxMessage;

		private void Page_Load (object sender, EventArgs e)
		{
			ButtonEvents events = new ButtonEvents(Context.Items);
			events.ImageButtonClicked += new ImageClickEventHandler(events_ImageButtonClicked);
			events.ButtonClicked += new EventHandler(events_ButtonClicked);
		}

		private void events_ImageButtonClicked (object sender, ImageClickEventArgs e)
		{
			uxMessage.Text =
				string.Format("Last event received from an image button at ({0}, {1})", e.X, e.Y);
		}

		private void events_ButtonClicked (object sender, EventArgs e)
		{
			uxMessage.Text = "Last event received from a button";
		}

		override protected void OnInit (EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent ()
		{
			Load += new EventHandler(Page_Load);
		}
	}
}

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)


Written By
Software Developer (Senior)
Norway Norway
I have programmed applications in ASP.NET from the very beginning. My favourite .NET language is C#, and I am a fan of Test Driven Design. I was an experienced VB6 coder before the .NET era. I have also done some Transacted SQL programming in Sql Server. Although I have made a career on Microsoft technology, I have browsed enough Java code to read it fluently, and I find Smalltalk and Ruby very interesting.

I live a few kilometers away from Oslo.

My email is thomas.eyde@gmail.com.

Comments and Discussions