Click here to Skip to main content
15,892,298 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.5K   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 SenderControl : UserControl
	{
		protected Button uxButton;
		protected ImageButton uxImage;

		private ButtonEvents events;

		private void Page_Load (object sender, EventArgs e)
		{
			events = new ButtonEvents(Context.Items);
		}

		private void uxButton_Click (object sender, EventArgs e)
		{
			events.ClickButton(sender, e);
		}

		private void uxImage_Click (object sender, ImageClickEventArgs e)
		{
			events.ClickImageButton(sender, e);
		}

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

		private void InitializeComponent ()
		{
			uxButton.Click += new EventHandler(uxButton_Click);
			uxImage.Click += new ImageClickEventHandler(uxImage_Click);
			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