Global Events

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
I don't think it's possible, at least I researched it and couldn't find anything. I want to add a procedure to the Page_Load of every page in a project. Rather than going to each page and adding the procedure to the 'Page_Load' event manually, and rather than losing my designer UI by inheriting from another base that inherites System.Web.UI.Page and having it there, I rather do something easy and just add it in the Global somewhere. Don't think it's possible, but just wanted to ask.

Thanks!
 
Sure there is.
I have a multiproject solution. 'HB' is a class library. I have another project that's asp.net project.
In the HB library exists the following file.
Code:
using System;

namespace HB.Web.UI
{
	public class Page : System.Web.UI.Page
	{
		static Page()
		{
		}

		public Page()
		{
			Load+=new EventHandler(Page_Load);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			try
			{
				VirtualPageLoad(sender, e);
			}
			catch(Exception error)
			{
				if(VirtualPageLoadError(sender, e, error))
					throw;
			}
		}

		protected virtual void VirtualPageLoad(object sender, EventArgs e)
		{
		}

		protected virtual bool VirtualPageLoadError(object sender, EventArgs e, Exception error)
		{
			return true;
		}
	}
}


Then I have an aspx page in another project.

Code:
<%@ Assembly Name="HB"%>
<%@ Import Namespace="ItemPageCreator" %>
<%@ Import Namespace="iCode" %>
<%@ Import Namespace="iCode.Collections" %>
<%@ Import Namespace="System.Text" %>
<%@ Page language="c#" Inherits="HB.Web.UI.Page"%>

<script runat="server" language="C#">
//STORES THE 'TOP 42' ITEMS
private Item[] Items;

protected override void VirtualPageLoad(object sender, EventArgs e)
{
	base.VirtualPageLoad(sender, e);
	SOME_LABEL.Text = "Looks good form here";
	//1 - SELECT THE TEMPLATE
	ItemPageTemplate template = ItemPageTemplate.GetTemplate("top42");
	//2 - GET THE ITEM NUMBERS
	string[] itemNos = template.GetItemNumbers();
	//3 - CREATE THE ICODE ITEM OBJECTS
	Items = Item.FromItemCodes(itemNos);
	//4 - OPTIONAL - SORT THE ITEM OBJECTS BY WHATEVER WAS CHOSEN IN THE ITEM PAGE TEMPLATE
	Array.Sort(Items, template.ItemComparer);
}

static string SSW(string value)
{
	return string.Format("<%{0}%>", value);
}

static string HSSW(string value)
{
	return HttpUtility.HtmlEncode(SSW(value));
}

</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>TOP42Test</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body>
		<table>
			<%
  //!-- ITERATE THROUGH EACH ITEM AND DISPLAY THE ITEM NUMBER
  foreach(Item item in Items)
  {%>
			<tr>
				<td>
					<%=item.ItemNumber%>
				</td>
			</tr>
			<%
  }%>
		</table>
		<%=SSW("NOT HTML ENCODED")%>
		<%=HSSW("HTML ENCODED")%><br>
		<asp:Label id="SOME_LABEL" runat="server">***</asp:Label>
	</body>
</HTML>

<%@ Page language="c#" Inherits="HB.Web.UI.Page"%> tells it to derive from my custom page class and no code behind is generated or needed.
 
Thanks... I think my question was confusing.

I want to do the following WITHOUT using Inheritance.

In a page you may have: Page.Load += New EventHandler(DelegateSignature)
I want to be able to do this for all pages from one place...say the global:

((Page)sender).Load += New EventHandler(DelegateSignature)

See what I'm saying? As I said, I don't think it's possible, but wanted to ask. Using and interface is nice, but I still have to go around to each page and add the interface...I'm trying to avoid touching the Page code behinds totally from the start.
 
1 alternative is to use the global asax and do whatever it is you wanted to do when the BeginRequest event was raised, but you won't have access to the 'Page' object, only the Request and Response. Does this help you any?
 
Back
Top