Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:07:02+00:00 2026-06-09T12:07:02+00:00

Surely it must be a common problem but I can’t find an easy way

  • 0

Surely it must be a common problem but I can’t find an easy way to do this.

Is there a way to share a global variable (say a class which is expensive to instantiate) through the life cycle of an asp.net webform, without having to pass the handle to every single component of the page?

If I just create a static variable, it will be accessible by all threads in the app domain (problem: my class is not thread safe), and it will be hard to ensure that every page works on a recent copy (I want to cache the class through every step of the life cycle of the page, but I want a new instance every time a new page is called).

The alternative is to pass a handle through each control in the page, but between the master page, the page itself, and all the user controls, it makes the code quite hairy.

I was wondering if there wasn’t an elegant solution to store a class in some place which is accessible only to the thread executing this particular page (including all sub user controls)?

Any suggestion welcome!
Thanks
Charles

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:07:04+00:00Added an answer on June 9, 2026 at 12:07 pm

    There is a simple answer: the Items container. It is available for the lifetime of a single request and then it is automatically destroyed. You can wrap it in a property to have what you want:

     public class Foo {
    
       const string SOMEKEY = "_somekey";
    
       public static string SingleRequestVariable
       {
          get
          {
              return (string)HttpContext.Current.Items[SOMEKEY];   
          }
          set
          {
              HttpContext.Current.Items.Add( SOMEKEY, value );
          }
       }
     }
    

    and then

    Foo.SingleRequestVariable = "bar"; // somewhere
    ...
    string val = Foo.SingleRequestVariable; // yet somewhere else
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Surely there must be a way to do this... I can't work it out.
This surely is a noob question, but I can't find an answer in Doxygen
This must be quite a common problem. An application depends on Library A and
I have what must surely be a fairly common documentation need... I'm implementing a
I have coded the next function. But surely someone has a more elegant way
I can't believe this isn't possible. It's just one of those things that surely
I have a question that must surely seem very trivial, but the answer has
This may be a simple question but I can't seem to get my head
What is following will surely appear very simple for c coders but I am
I'm surely not the first one noticed the problem below: when using the graphical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.