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

  • Home
  • SEARCH
  • 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 6327007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:12:04+00:00 2026-05-24T17:12:04+00:00

Well, i’m working in a asp.net 3.5 site. I have set an Observer like

  • 0

Well, i’m working in a asp.net 3.5 site.

I have set an Observer like this:

public delegate void ActionNotification();

protected Dictionary<string, List<ActionNotification>> Observers
{
    get
    {
        Dictionary<string, List<ActionNotification>> _observers = Session["Observers"] as Dictionary<string, List<ActionNotification>>;
        if (_observers == null)
        {
            _observers = new Dictionary<string, List<ActionNotification>>();
            Observers = _observers;
        }
        return _observers;
    }
    set
    {
        Session["Observers"] = value;
    }
}

public void Attach(string actionName, ActionNotification observer)
{
    if (!Observers.ContainsKey(actionName))
    {
        Observers.Add(actionName, new List<ActionNotification>());
    }
    Observers[actionName].Add(observer);
}

public void Detach(string actionName, ActionNotification observer)
{
    if (Observers.ContainsKey(actionName))
    {
        Observers[actionName].Remove(observer);
    }

}
public void DetachAll(string actionName)
{
    if (Observers.ContainsKey(actionName))
    {
        Observers.Remove(actionName);
    }
}

public void Notify(string action)
{
    if (Observers.ContainsKey(action))
    {
        foreach (ActionNotification o in Observers[action])
        {
            o.Invoke();
        }
    }
}

I use the observer like this:

//Esta es llamada al notify con cierto action       
protected void btnNext_Click(object sender, ImageClickEventArgs e)          
{           
    Notify("Next");         
} 
//Y este es el register del Listener            
Attach("Next", new ActionNotification(NextButton_Click)); 

If before the o.Invoke(); for example i change the page title to “Hello”.
And inside the “NextButton_Click” I set it to “Goodbye”, after the NextButton_Click finish, the Title goes back to “Hello”…

Any idea why?

  • 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-24T17:12:06+00:00Added an answer on May 24, 2026 at 5:12 pm

    I think problem is that the “Page” in your NextButton_Click event is not the same page as the page you set the title to “Hello” on. Because you are passing around events in the session when the event is raised the object is acts on is no longer in scope. You can recreate it with the following code (which is using EventHandlers, but they are basically the same as what you have outlined in your code)

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Title = "test";
    
        //Store it in your session...seems like a weird thing to do given how your page should be stateless, so I would think about what you are
        //trying to do a bit more carefully.  You don't want to call an event handler such as test below from another page in your asp.net app.
        Dictionary<string, EventHandler> myEvents = null;
        if (Session["Invokers"] == null)
        {
            myEvents = new Dictionary<string, EventHandler>();
            Session["Invokers"] = myEvents;
        }
        else
        {
            myEvents = Session["Invokers"] as Dictionary<string, EventHandler>;
        }
        //If the event handler key is not in there then add it
        if (myEvents.ContainsKey("buttonClickOnPageDefault") == false)
        {
            //Subscribe to event (i.e. add your method to the invokation list
            this.TestEvent += new EventHandler(test);
            myEvents.Add("buttonClickOnPageDefault", this.TestEvent);
        }
        else
        {
            //if it does contain this key then you may already be subscribed to event, so unsubscribe in case and then resubscribe...you could
            //probably do this more elegantly by looking at the vales in the GetInvokationList method on the eventHandler
            //Wire up the event
            this.TestEvent -= new EventHandler(test);
            this.TestEvent += new EventHandler(test);
        }
        //Resave the dictionary.
        Session["Invokers"] = myEvents;
    }
    
    void test(object o, EventArgs e)
    {
        this.Page.Title = "testEvent";
    }
    
    public event EventHandler TestEvent;
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["Invokers"] != null)
        {
            Dictionary<string, EventHandler> myEvents = (Dictionary<string, EventHandler>)Session["Invokers"];
            if (myEvents.ContainsKey("buttonClickOnPageDefault"))
            {
                EventHandler ev = myEvents["buttonClickOnPageDefault"];
                ev(null, EventArgs.Empty);
            }
        }
    }
    

    If you put the above code in an asp.net page it will never change page title, but if you put a breakpoint in the Test method you will see it being hit. The reason is that its being hit in a different page (and that page is out of scope and may not be garbage collected as your event still has a reference to it, so this could cause a memory leak…be careful with it!). Really you probably shouldn’t be using your events this way (at least not to act on a page…maybe it has some utility for domain objects). Note that the following will work (as its acting on the same page)

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Title = "test";
    
        //Store it in your session...seems like a weird thing to do given how your page should be stateless, so I would think about what you are
        //trying to do a bit more carefully.  You don't want to call an event handler such as test below from another page in your asp.net app.
        this.TestEvent += new EventHandler(test);
        Session["Invoker"] = this.TestEvent;
    }
    
    void test(object o, EventArgs e)
    {
        this.Page.Title = "testEvent";
    }
    
    public event EventHandler TestEvent;
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["Invoker"] != null)
        {
            EventHandler ev = (EventHandler)Session["Invoker"];
            ev(null, EventArgs.Empty);
        }
    }
    

    Hope that gives you some pointers to where your problem might be.

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

Sidebar

Related Questions

Well, this is an interesting problem. I have an ASP.NET MVC3 Intranet application running
well i have a configuration like this in the components part of my config
Well, this is my first post here and really enjoying the site. I have
Well, I'm working on an online game in Java. I have a client and
Well im trying to setup a net.tcp connection with SSL encryption I have the
Well, I have this bit of code that is slowing down the program hugely
Well, i have such-like code that prevent select all action from keyboard: $(document).keydown(function(e){ //
Well the problem is that I was using code like this: new Date().toJSON().slice(0, 10)
Well friends, I have got this query which works but is very long for
Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection

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.