How to show messenger-like popups using Ajax

Source: Internet
Author: User

    • Download source code-212 KB
Introduction

You have already been using MSN Messenger to chat with friends, relatives, businesses, and much more. when someone signs in, a small popup window shows up on the right-bottom side of the screen, informing you that your friend X has just signed in. it is a nice way for MSN Messenger to keep y you about changes in the contacts online.

What about using the same technique on an ASP. NET page? In other words, suppose that you are monitoring a database table where users place their orders online, and you wocould like to be notified whenever a new request comes in to the database table.

In this article, we will see how to implement a messenger-like popup window whenever a new record is added to a database table. the popup will show on the bottom-Right side of the screen on top of an ASP. NET page, policying you that a new record has been added. we will use ASP. NET 2.0 Ajax 1.0 extensions and, mainly, the client library that accompanies with the Ajax extensions.

Background

In this article, we will use a database table calledMyinboxSimulating your inbox on a mail server. This table containsEmailfrom,Body, AndDateColumns. These columns shall resemble an email that you have stored ed in your inbox as an example.

Using the codeconstructing the Javascript

We will create a new Ajax enabled website, then openDefault. aspxPage in the HTML source, and start adding our JavaScript that will handle showing and hiding the popup and querying the database to see if there are any new emails inserted.

First of all, we will define a global variable.

Listing 1: Collapse Copy code
 
VaRNumberofemails_original =0;

TheNumberofemails_originalVariable shall hold the number of emails present in the database table when the page first loads. We will see later on when and how this variable is used. Then, we attach an event handler forApplication'SInitMethod.

Listing 2: Collapse Copy code
 
SYS. application; app. add_init (applicationinithandler );FunctionApplicationinithandler (sender, argS) {inboxservice. getlatestnumberofemails (oncurrentnumberofemailsready );}

What we are doing at the initialization ofApplicationIs do a script service call to executeGetlatestnumberofemailsMethod located at the Ajax service that we will discuss later on in the article. We are specifying the success callback function to run when a response is sent back from the server.

The idea in here is that, the first time the page loads, we get the current number of emails from the server and store it inNumberofemails_original, As shown inOncurrentnumberofemailreadyCallback function below.

Listing 3: Collapse Copy code
 
FunctionOncurrentnumberofemailsready (result, usercontext, methodname) {numberofemails_original = result;//Start checkingStartchecking ();}

As you can see, we set the result returned by the Ajax service to the local variableNumberofemails_original, And then call a method namedStartchecking ().

Now that the original number of emails currently stored in the database is known, we can start checking from now on for any new email that is added to the database table.

TheStartcheckingMethod is as follows:

Listing 4: Collapse Copy code
 
FunctionStartchecking () {inboxservice. getlatestnumberofemails (onlastestnumberofemailsready );}

TheStartcheckingFunction does nothing but a call to the same Ajax service method,Inboxservice. getlastestnumberofemails. However, this time, we are passing in another success callback function, which isOnlatestnumberofemailsready. The success callback function is shown below.

Listing 5: Collapse Copy code
 
FunctionOnlastestnumberofemailsready (result, usercontext, methodname ){VaRNumberofemails_new = result;If(Numberofemails_new> numberofemails_original) {showpopup (); $ get ("Modalbody"). Innerhtml = numberofemails_new-numberofemails_original;//Update the count hereNumberofemails_original = numberofemails_new ;}//Start checking againWindow. setTimeout (startchecking,10000);}

The method starts by placing the result from the Ajax service into a local variable calledNumberofemails_new. This variable now holds the latest number of emails located in the database table.

Then, the code checks if the number of emails currently stored in the database is greater than the number of emails originally the page had requested from the database. this means that new emails we have were inserted into the database since the last time the Code queried the database.

if this is true, then a call to the showpopup () function is done that is responsible for showing a popup the MSN Messenger way. then, the number of new emails is filled in the body of the popup window, informing the user on the number of new emails encoded ed, and finally, the Code sets the current number of emails in the database table to the numberofemails_original variable. this way, the numberofemails_original variable is synchronized with the emails 'Number in the database. refreshing the value of this variable is very important for later checking on the client side.

The last statement in this method is a call forStartcheckingMethod encapsulated inWindow. setTimeoutFunction call. As you can see here, the same logic will run according to the milliseconds placed inSetTimeoutFunction. so, as long as the page is running, the checking will continue, and every time a new email is added to the database table, A pop up window is shown on the screen to your y the user about the new emails that have been added.

The figure below shows the page when it first loads.

Figure 1: page when first loaded

 

When a new record is inserted into the database while the above page is opened, you will notice a small messenger like pop up window is shown on the bottom-Right side of the page, as in Figure 2.

Figure 2

 

You can clearly see a popup window in the figure above that shows when the page checks the database table on the server and finds that the number of emails currently on the database is greater than the number emails stored on the client side.

The last two functions to discuss areShowpopupAndHidepopupThat are used as utility methods to show and hide the popup window on the page.

Building the Ajax Service

In this final section of the article, we will see the simple Ajax service that we have created that the client code will use to access the database and retrieve information about the number of records stored in the database.

Listing 6: Collapse Copy code
[Scriptservice] Public  Class Inboxservice: system. Web. Services. WebService {[webmethod] Public   Int Getlatestnumberofemails (){ Int Numberofemails = 0 ; Using (Sqlconnection conn = New Sqlconnection (webconfigurationmanager. connectionstrings [ 0 ]. Connectionstring )){ Using (Sqlcommand cmd = New Sqlcommand ( " Getlatestnumberofemails" , Conn) {cmd. commandtype = commandtype. storedprocedure; Conn. open (); numberofemails = ( Int ) Cmd. executescalar ();}} Return Numberofemails ;}}

The first thing to notice isScriptserviceAttribute located at top of the service name. This is very important if you want your web service to be a script callable service. The above Web Service implements des a web methodGetlatestnumberofemailsThat accesses the database and retrieves the total number of emails or records in the database.

 

Reprinted: http://www.codeproject.com/KB/ajax/ASP_NET_MSN_Modal_Popup.aspx

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.