Kaa
Kaa

Reputation: 21

ASP.NET Updating a UpdatePanel on another client

Okay. So basically i am working on a message system on a webpage. Users on my webpage is able to send each other messages, but now i want the messages to "pop up" on the receivers screen when sent. Exactly like when somebody on facebook sends you a message while your online, the message thing goes red. To solve my problem i need every client to know which other clients are online at the moment. I have solved this by coding an Observer-like pattern in my Global.asax:

    public static void AddObserver(Observer o)
    {
        if(!observers.Contains(o))
            observers.Add(o);

        System.Diagnostics.Debug.WriteLine("Observer tilføjet : " + observers.Count);
    }

    public static void RemoveObserver(Observer o)
    {
        if (observers.Contains(o))
            observers.Remove(o);

        System.Diagnostics.Debug.WriteLine("Observer fjernet : " + observers.Count);
    }

    public static void NotifyObserversNewMail(Observer observer)
    {
        foreach (Observer o in observers)
            if(!o.Equals(observer))
                o.UpdateNewMail();
    }

And the observer in this case i simply the Site.Master, which i have made extend the Observer class :

public partial class SiteMaster : System.Web.UI.MasterPage, Observer
{
    protected void Page_Unload(object sender, EventArgs e)
    {
        Session["observer"] = this;
        Global.AddObserver(this);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //ADD OBSERVER TO GLOBAL.ASAX
        if (Session["observer"] != null)
            Global.RemoveObserver((Observer)Session["observer"]);

    public void Update()
    {
        DLMessages.DataSource = ServiceMessages.GetInstance().GetMessages();
        DLMessages.DataBind();

        UPMessages.Update();
    }

Where DLMessages is a DataList inside the UpdatePanel UPMessages.

So we have a "sender" client, and a "receiver" client. When the sender creates a new message this method gets called:

    protected void MessageSend(object source, EventArgs args)
    {
        Page.Validate("ValGroupMessageTo");
        if (Page.IsValid)
        {
              ServiceMessages.GetInstance().SendMessage(ServiceCommunity.GetInstance().GetUser(MessageTo.Text).Id, ((User)Session["user"]).Id, MessageMessage.Text);

            Global.NotifyObserversNewMail((Observer)Session["observer"]);

            ClosePopups(new object(), new EventArgs());

            Update();
        }
    }

As you can might notice it calls the Notify on global.asax, and the update() directly on itself. The UpdatePanel on the "sender" side updates perfectly, but on the receiver side nothing happens. Not in the UpdatePanel anyways. Cause if i alter the code in the Update() to run through the messages from the DB, i can see that the message gets called fine, and the new message is loaded. Just not updated to the UpdatePanel. So i have been thinking a lot about why the updatepanel doesnt get updated on the "receiver" side when the data gets updated, and my conclusion is it is because theres no partial postback on the "receiver" side. Yeah sure, the Update() method gets called, but theres no postback. So my question is this: Is it possible to "force" a partial post back from the code-behind? Or is there another solution that might work better?

Hope it makes sense :-)

Upvotes: 2

Views: 361

Answers (4)

LandRyu
LandRyu

Reputation: 1

I'm not an expert but you could try the Javascript/Jquery SetTimeout function associated with a WebService (.svc) to make periodic request from the client and retrieve data from the server.

Upvotes: 0

Stilgar
Stilgar

Reputation: 23551

Do yourself a favour and build the whole thing using SignalR. It is a library for real time communication between the server and the browser for .NET (includes client-side libraries).

If you insist on doing it the hard way you should use a timer to trigger the update panel update

Upvotes: 1

Jordan
Jordan

Reputation: 2758

Those solutions, comet etc will work but really your best bet is to use socket.io Real Time Browser Applications You could also combined it with backbone.js and have a really nice app. I helped make a real time web messenger based on these 2.

Upvotes: 0

Maarten van der Lee
Maarten van der Lee

Reputation: 33

I'm not a big fan of working with updatepanels, but I think you can use the timer-control to force a partial postback.

Have a look at: http://mattberseth.com/blog/2007/08/using_the_ajax_timer_control_a.html

Upvotes: 0

Related Questions