George
George

Reputation: 2200

How can I share data between multiple user controls?

I have 3 user controls, each one of which will need to pull data from an XML feed based on various URL parameters and such. Now the data in this XML feed will be identical for all 3 user controls, because they're all sending the same request.

It seems silly to make 3 separate requests for what will be identical data.

My idea is to have a singleton that I'll call which will do one request to the XML service and then present nicely parsed results to the user controls.

i.e.:

  1. User control A grabs the singleton.
  2. User control A asks the singleton to make a request...
  3. Singleton makes the request and parses the XML into various properties.
  4. User control C grabs the singleton and pulls its data out.
  5. User control A pulls its data out of the singleton.
  6. User control B grabs the singleton and pulls its data out.

Now that I think about it, I'll need to add some sort of blocking to the "make a request" method so that I'm not calling it multiple times concurrently — I'm fairly new to C#, what's the easiest way to do this? Do user controls even run concurrently? Is there a nice overview for this somewhere?

The question is "will a singleton be reinstantiated in between page requests in C#?", but I guess a more general question would be "am I doing a silly thing", "what's the nicest way to do this?" or "is there an easier way to do what I want?".

Upvotes: 3

Views: 2544

Answers (3)

Thomas
Thomas

Reputation: 64655

will a singleton be reinstantiated in between page requests in C#?

No. As long as the class is in memory, a static value will persist across page requests, sessions and users. It is akin to putting something in Application cache. A better solution would first be session (which is per user), then HttpCache (notably because of the ability to expire values) and lastly (if you absolutely must) a true singleton. If this data is to be recreated per page, then build it in the Page_Load as MattMitchell suggested.

Upvotes: 1

Matt Mitchell
Matt Mitchell

Reputation: 41833

As a general rule, if my instinct is a Singleton I assume I'm doing it wrong (as you likewise intuited).

If you want one XML request per Page Request, and the data to be shared amongst controls, I'd do something like this:

public class Page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        string data = DoXMLRequest();
        ucControl1.Data = data;
        ucControl2.Data = data;
        ucControl3.Data = data;
    }
}

Ensuring that each control has a public property of the type returned by your XML Request code (in this example a string).

For reference:

The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.

From: ASP.NET Page Life Cycle Overview (which should serve as a nice overview).

Upvotes: 6

Related Questions