SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • LINQ (4)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box
ASP.NET Hosting with MS SQL 2008 – Click Here!

Connect Your Site With HttpContext Items

(May 06 2007 - 03:36:44 PM by Timothy Khouri) - [print article]

ASP.NET gives developers a lot of powerful tools to seperate functionality into Master Pages, Custom Controls, User Controls and Content Pages allowing clean and segragated application design. The problem comes in when you actually WANT two pages to talk to each other. A common scenario is when you have a control, such as a DropDownList, on one part of the page that needs to filter results on another part of the page that is not directly accessable (meaning it's in a UserControl, or part of the MasterPage and you are in another UserControl or the ContentPage). This article will describe how to easily tie your entire web application together without running into cross-threading issues due to using static objects.

The Problem With Static

First we want to discuss why you can't just use a bunch of static objects to 'cheat' the system. Because web applications are multi-threaded, meaning every person that hits your site is running his own "instance" of your web site, you can really step on your toes if you use static objects to set a value, then retrieve it later in the page cycle. This is because another user may hit your site in the meantime and changed that value to something else when you didn't expect it! Even if you test your site a thousand times, you'll never see this issue until you launch your web application, because you are just one person hitting your site.

// This is an example of a property called "SelectedArticle" that
// is accessable to your entire web application. The problem here

// is that you will run into issues when two or more users are on

// your site at the same time, and they both are using that same

// "SelectedArticle" property.


public static class SiteObjects
{
   private static string selectedArticle;
   public static string SelectedArticle
   {
       get { return selectedArticle; }
       set { selectedArticle = value; }
   }
}

Static Accessors To Instance Objects

So the answer to our problems is to tie into the HttpContext.Items that is associated with current request. The HttpContext.Items is a collection of properties that you can set anything inside of, and it will be accessable in any page or control of your web application. It is thread safe because every thread gets it's own 'bucket' to put things in!


// This is almost just like the example above except that now

// we are going to use the HttpContext Items so that we will be

// completely thread safe.


public static class SiteObjects
{
   public static string SelectedArticle
   {
       get { return HttpContext.Current.Items["selectedArticle"] as string; }
       set { HttpContext.Current.Items["selectedArticle"] = value; }
   }
}

You can extend this very simple "SiteObjects" file to include things like your own custom events that you can tap into. Let's use that example above about needing to filter results from one UserControl to another. Here we are going to setup our own custom event so that we communicate from one control to another.

public static class SiteObjects
{
   // This is our list of events that is tied to the current

   // HttpContext, so it is thread safe.

   private static EventHandlerList CategoryChangedEvents
   {
       get
       {
           if (HttpContext.Current.Items.Contains("categoryChanged") == false)
           {
               HttpContext.Current.Items["categoryChanged"] = new EventHandlerList();
           }

           return (EventHandlerList)HttpContext.Current.Items["categoryChanged"];
       }
   }

   // This is our own custom event that we will tap into.

   public static event EventHandler CategoryChanged
   {
       add
       {
           SiteObjects.CategoryChangedEvents.AddHandler(HttpContext.Current, value);
       }

       remove
       {
           SiteObjects.CategoryChangedEvents.RemoveHandler(HttpContext.Current, value);
       }
   }

   // Here is the method that you call in order to fire the

   // event all across your web app.

   public static void RaiseCategoryChangedEvent(object sender, EventArgs e)
   {
       EventHandler handler = (EventHandler)SiteObjects.CategoryChangedEvents["categoryChanged"];

       if (handler  != null)
       {
           handler(sender, e);
       }
   }
}

So in conclusion, if you need to tie your web application together, but you're not quite ready to create a "three-tier" enterprise level application, you can use this simple approach. If you are using ASP.NET 2.0, then you'll put this code inside your App_Code folder under it's own code file. With this example above you can tap into your needed information all over your site; in your UserControls, MasterPages, ContentPages and anywhere else you may need to.

  • Nov 23 2007 - 03:31:06 PM amassani

    Could you please review the Handler - RaiseCategoryChangedEvent(). Current code will always return the handler as null. It should be (EventHandler)SiteObjects.CategoryChangedEvents[HttpContext.Current].

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

People to Follow

Experts in the categories related to this article.

  • Jonathan Carter

Related Blogs

These are the most recent blog posts related to this article.

  • A Change to the MVC "ActionSelectionAttribute"?
  • How to Handle "Side Content" in ASP.NET MVC
  • LINQ to SQL - Am I Hitting The Database?
  • ASP.NET MVC - Issue with CSS Class Name on ActionLinks
  • Site Updates, Bug Fixes and Syndication in .NET 3.5

Related Ads

SingingEels.com as of Jan 05 2009 - 08:12:43 PM - (0.2499936)