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 6340567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:51:35+00:00 2026-05-24T19:51:35+00:00

Is there a way to force methods to be accessible only during certain events

  • 0

Is there a way to force methods to be accessible only during certain events during the page life cycle. For example, I have a extension to System.Web.UI.Page that adds a PrependTitle method.

I also have a masterpage that embeds another masterpage. The first masterpage sets the base title (Google), the next masterpage prepends the title (Calendar), and a page also prepends the title (21 May 2011).

The result should be:

21 May 2011 :: Calendar :: Google

And this is the case when the PrependTitle is run during the Page_Init event. However, when the method is run during Page_Load the following the results:

Google

So, that brings me to the question: How can it be enforced that a method only be accessible during specified life cycle events?

// The Method Mentioned
public static class PageExtensions
{
    public static void PrependTitle(this Page page, string newTitle)
    {
        page.Title = newTitle + " " + Global.TITLE_DELIMITER + " " + page.Title;
    }
}
  • 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-05-24T19:51:36+00:00Added an answer on May 24, 2026 at 7:51 pm

    If you want to brute force ensure that the method is being called from Init, you can inspect the call stack. Something like this:

    public static bool CalledFromInit()
    {
        //Grab the current Stack Trace and loop through each frame
        foreach(var callFrame in new StackTrace().GetFrames())
        {
            //Get the method in which the frame is executing
            var method = callFrame.GetMethod();
    
            //Check if the method is Control.OnInit (or any other method you want to test for)
            if(method.DeclaringType == typeof(Control) && method.Name == "OnInit")
                //If so, return right away
                return true;
        }
    
        //Otherwise, we didn't find the method in the callstack
        return false;
    }
    

    Then you would use it like:

    public static void PrependTitle(this Page page, string newTitle)
    {
        //If we aren't called from Init, do something
        if (!CalledFromInit())
        {
            //We could either return to silently ignore the problem
            return;
    
            //Or we could throw an exception to let the developer know they 
            //   did something wrong
            throw new ApplicationException("Invalid call to PrependTitle");
        }
    
        //Do the normally processing
        page.Title = newTitle + " " + Global.TITLE_DELIMITER + " " + page.Title;
    }
    

    However, I’d caution that the stack trace isn’t the most reliable thing. In release, code could get optimized such that the Control.OnInit method is inlined so your code wouldn’t be able to see it in the call stack. You could wrap this check in an #if DEBUG block so it only executes during development. Depending on your use case, it might be good enough to catch this problem while in DEBUG and not bother doing the check in RELEASE. But that’s up to you.

    Another option…building on Tommy Hinrichs answer, if all your pages inherit from a base class, you’ll be able to do it a bit more reliably. I’d suggest something like this:

    public abstract class BasePage : Page
    {
        private bool _executingInit;
        protected internal override void OnPreInit(EventArgs e)
        {
            _executingInit = true;
            base.OnPreInit(e);
        }
    
        protected internal override void OnInitComplete(EventArgs e)
        {
            base.OnInitComplete(e);
            _executingInit = true;
        }
    
        public void PrependTitle(string newTitle)
        {
            if (!_executingInit)
                throw new ApplicationException("Invalid call to PrependTitle.");
    
            Title = newTitle + " " + Global.TITLE_DELIMITER + " " + Title;
        }
    }
    

    That way, PrependTitle will throw an exception unless it’s called between PreInit and InitComplete (which sounds like exactly what you want).

    As one last option, you could be sneaky and use reflection to access the Control.ControlState property (which is a confusing name because it’s not related to Control State – the thing similar to View State). That property tracks the Control as it goes throw its lifecycle – and it has the following values:

    internal enum ControlState
    {
        Constructed,
        FrameworkInitialized,
        ChildrenInitialized,
        Initialized,
        ViewStateLoaded,
        Loaded,
        PreRendered
    }
    

    You’ll notice that Enum is internal. So is the Control.ControlState property. But with Reflection, you could use that – and you could even use it from an extension method that is external to the Page.

    Hope one of those ways will work for you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to force Joda time to parse dates only when they
Is there any way to force a listview control to treat all clicks as
Is there any way to force an update of software using RunOnce, without having
Is there a way to force the flash garbage collector to clean up freed
Is there any way to force Outlook to display HTML in the desktop alert
Is there any way to force Text-Mate to use a two-space tab instead of
Is there a way to force IE or FF into a handheld mode for
Is there any way to force PHP to blow up (error, whatever) if I
Is there a way to force use of a named route in ASP.NET MVC
Is there any way to force Visual Studio to copy selected code to the

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.