Search code examples
asp.netevent-handlingpostbackcode-behindlync

ASP.NET: How to update frontend within a subscribed event handler


The situation

I have an ASP.NET Webform that contains some status information about a users Microsoft Lync 2010 session. In the Page_Load event i registered an event handler that listenes for a Lync session status change event.

Once the event fires, the event handler reacts correctly and does its job: it changes the according text of a specific label. The problem is, the changes are not visible in the front end webpage.

The code

This is a global Lync Client instance, defined in the page.

private LyncClient _lyncClient;

Here I am registering the LyncClient in the Page_Load event.

protected void Page_Load(object sender, EventArgs e)
{
    RegisterLyncClient();
}

I assign the LyncClient instance to the global _lyncClient. After that I register my event listener.

private void RegisterLyncClient()
{
    _lyncClient = LyncClient.GetClient();
    _lyncClient.StateChanged += HandleStateChanged;
}

This function is fired correctly as the Lync Client changes its state. Its visible during Debug mode.

private void HandleStateChanged(object sender, ClientStateChangedEventArgs e)
{
     SetLyncStatus(e.NewState);
}

The labels text is changed correctly (during debug mode). But the changes are not visible in frontend.

private void SetLyncStatus(ClientState lyncState)
{
    lblLyncStatus.Text = lyncState.ToString();
}

Assumption

If I use a button to manually change the status or if I use a timer to check and adjust the status every second, the changes are visible. So I assume the changes made within the HandleStateChanged event handler are not visible, because there is no postback after calling the SetLyncStatus function.

Question

Is it possible to force a postback from an event handler, that is not fired from a users action (e.g. user clicks a button)? It would be perfect, if its possible to call a partial refresh of the label or a defined section.

Any suggestions much appreciated. Thanks!


Solution

  • Before you try to implement any solution try to understand what runs on client what on server. Maybe you have a desktop development background and hence you also need to understand the stateless nature of web. Any changes happening on the server side would not reflect on client side and vice-versa. Once the server has responded to a request it would not automatically communicate status update to client. So a even driven approach would not work in your case. You need to do pings from client side to determine status changes. This pings can be done using AJAX with a predefined periodic interval.