1

I have an object, let's call it objK that has an event Message that accepts a string. I have a event handler in my code behind page called HandleObjKMessage(string s). that method looks like this currently :

void HandleObjKMessage(string s)
    {
        TextBox1.ReadOnly = false;
        TextBox1.Text += s+ Environment.NewLine;
        UpdateP.Update();

    }

objK is an object from an external .dll(assembly) that is running an iterative process that I need to be keeping the user update from. That is the purpose of using an event.

Am i barking up the wrong tree? I'm typically a winforms guy and this is frustrating the hell out of me.

Thanks.

kyle k
  • 11
  • 3

1 Answers1

0

I might have misunderstood you, but you have an object that you instantiate, and then add an event handler to this object's event, and when the event fires you want the user to get the updated data?

If this is the case, it is not possible. This is simply because the web does not have state. There is no persistent connection between the server and the client.

When a request is received for your page, the server will create an instance of your page class, and run through the life-cycle (init, load etc.) Once the page has been rendered into HTML markup, the server disposes the instance of the page, and thus your attached event handler.

You can read more about the WebForms Page Lifecycle at MSDN

If you really want to go all the way and actually push the notification to your user, you could use SignalR, but this adds quite some complexity.

  • You have answered my question. I was assuming this was the case though i'm still learning about "state" in terms of an asp page. What I don't understand is this : I can step-through the code behind and see that the event handler is hit each time. This confuses me why it would do this but not update the html page through AJAX. I will follow up with the links you sent. thanks. – kyle k Jun 04 '13 at 13:47
  • I wanted to add an additional question/comment : in my onClick event i can update the textbox, of course, but I was under the assumption that using an UpdatePanel would make asynchronouse calls to update the HTML on the client. – kyle k Jun 04 '13 at 13:56
  • Really? The event handler is still registered? That seems weird. Perhaps the page hasn't been disposed. Anyway, the reason it does not update through AJAX, is because HTTP is not a full duplex protocol. The client can do requests to the server, and the server can respond to these requests, but that's it. If you want, you can have a client side javascript that polls the server asynchronously every x seconds to see if the data has changed. Regardless, all these polling and SignalR pushing techniques will require you to have a good understanding of the lifecycle – Christoffer Mansfield Jun 04 '13 at 14:09
  • Yes, the HandleObjKMessage(string s) is getting hit (with correct info) in sync with the external process. It's simply the page isn't unpdating. I'm reading up on this stuff more thoroughly right now (which i should have done long ago) – kyle k Jun 04 '13 at 14:34
  • The fact that the `HandleObjKMessage` is hit is actually worrying, since it might mean the garbage collector cannot collect the instances of your page. Probably, since each and every instance of your page class are listening for a statically available event. This is a classic [event memory leak](http://stackoverflow.com/questions/3662842/how-do-events-cause-memory-leaks-in-c-sharp-and-how-do-weak-references-help-miti). The problem is since the web is solely request-response from the client, the model of events pushing data simply wont work – Christoffer Mansfield Jun 04 '13 at 19:28