japesu
japesu

Reputation: 134

asp.net running javascript function after event is handled

It could be totally wrong what I'm trying to do but my problem is that I have an email sending class that will raise an event when the email has been sent.

I'm using this class in my asp.net project and now I want some way to notify the user when the email sending process starts and when I get the response event I want to tell the user that the email has now been sent or that there was exception.

I can debug the asp.net page and I get the event from the class after a while and then I have created a sub that creates a JavaScript call that should simply replace content of a div element on the page.

Private Sub EmailStatus(ByVal status As String)
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "WriteEmailStatus", "WriteEmailStatus('" & status & "');", True)
    End Sub

The JavaScript:

function WriteEmailStatus(status) {
            document.getElementById('emailStatusDiv').innerHTML = status;
        }

So debuggin tells me this sub is run after the event but the div content remains unchanged. If running this same sub in the page_load for example the div content changes.

Does anyone know what is the problem here?

Upvotes: 0

Views: 72

Answers (2)

Subin Jacob
Subin Jacob

Reputation: 4864

If you want to send anything from server to client you need to invoke client by server. Currently this method is made possible by workarounds like long polling or by using HTML5 WebSockets. But if you try to write code from scratch, you would have to do much optimisations and fall backs. Using SignalR will do all these tasks for you.

Use SignalR in such cases.

Alternatively, an easier method would be using WebMethod. So send mail by making an ajax call to this WebMethod. Also the mail should not be sent asynchronously. If you are new to WebMethod please refer this.

Upvotes: 1

Marius George
Marius George

Reputation: 526

Simply use SignalR. I'm sorry for the short answer - I will edit this later to elaborate.

http://signalr.net/

Upvotes: 0

Related Questions