alexandermarcelo
alexandermarcelo

Reputation: 55

I need to communicate between 2 pages without refreshing the page, with ajax, when a event is raised on the serverside

To clearify, I've tried solutions with updatepanel/scriptmanager but I do NOT want to use them. I want to make this jquery-ajax posts and nothing else.

Lets assume this scenario.

We have a global event as follows:

public static class CustomGlobalEvents

{

    public delegate void SpecialEventRaisedEventHandler(object sender, string type, string msg);

    public static event SpecialEventRaisedEventHandler SpecialEventRaised;

    public static void SpecialEvent(object sender, string type, string msg)
    {
        if (SpecialEventRaised != null)
            SpecialEventRaised(sender, type, msg);
    }

}

We have 2 web-pages, reciever.aspx and sender.aspx.

reciever.aspx has the following code:

protected void Page_Load(object sender, EventArgs e)

{

    if(!IsPostBack)
    {
        CustomGlobalEvents.SpecialEventRaised += new CustomGlobalEvents.SpecialEventRaisedEventHandler(CustomGlobalEvents_SpecialEventRaised);
    }

}

void CustomGlobalEvents_SpecialEventRaised(object sender, string type, string msg)
{
    // do something
}

sender.aspx has the following code:

protected void Page_Load(object sender, EventArgs e)
{

    if(!IsPostBack)

    {
         CustomGlobalEvents.SpecialEvent(this.Page, "type", "msg");
    }
}

What I'd like to do in the reciever.aspx event handler for the special event CustomGlobalEvents_SpecialEventRaised is that I want to fire a javascript on the reciever.aspx page.

Like doing a jquery ajax post and updating the information on the page.

I have tried with injecting a javascript to a scriptmanager but since the scriptmanager is not in a async state (even if I force a update with updatePanel.Update();) the script does not get to the page.

Another idea was to try to initiate a generichandler directly, but this resulted in sender.aspx getting the response instead of the reciever with the following code:

MyGenericHandler myGenericHandler = new MyGenericHandler();
myGenericHandler.ProcessRequest(HttpContext.Current);

which might make sense since its the sender.aspx that actually fired the event in the first place.

With this, nothing happens at all:

MyGenericHandler myGenericHandler = new MyGenericHandler();
myGenericHandler.ProcessRequest(this.Context);

The best method would obiously be to fire the generichandler and in some way make the reciever.aspx page recieve the response. The next best thing would be to tell reciever.aspx to fire a javascript method.

I've tried to create a httpmodule to try to hook something up, but failed.

Best other solution I've seen so far is to actually by jquery do a ajax post every x seconds. But this is defenatly NOT what I want. I want to fire it when it needs to be fired (when the server-side event fires).

Solutions working with IIS6 or IIS7, any is fine, but its ASP.NET 3.5 and not MVC.

Any ideas?

Edit: apparently doesn't stackoverflow's -tag public delegates... :)

Upvotes: 0

Views: 288

Answers (1)

Ernesto
Ernesto

Reputation: 1592

I don't think thats possible. For a response to exist there must be a request. Server cannot push something to the client. Your client code has to check for server status.

Upvotes: 2

Related Questions