ASP.net 2.0 in the implementation of the window alarm tips

Source: Internet
Author: User

In the Web application, such as OA, often need to use some hints, such as email arrived, do a hint box like MSN, pop-up to the user prompts, and then close. In the Ajax of ASP.net 2.0, this is not difficult to do now, just see a foreigner's article, explained to, the following summary

For example, there is a database table, is stored email, when the database table in the email one time, prompts the user, first simple write a webservice as follows

The following are the referenced contents:
[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;
}
}

Notice here to call Webserice on the client via Ajax, plus [ScriptService]

2 in Default.aspx, first add a UpdateProgress control, as follows

The following are the referenced contents:
<asp:updateprogress dynamiclayout= "False" id= "UpdateProgress1" runat= "Server" >
<ProgressTemplate>
<div id= "modal" class= "modal" >
<div class= "Modaltop" >
<div class= "Modaltitle" >my inbox</div>
<span style= "Cursor:hand" onclick= "Javascript:hidepopup ();" >

</span>
</div>
<div class= "Modalbody" >
You received <strong><span id= "Modalbody" ></span></strong> Email (s).
</div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>

Here's the close X button, call JavaScript script, wait for the array to say

Then of course you have to add the ScriptManager control, as follows

The following are the referenced contents:

<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
<Services>
<asp:servicereference path= "~/inboxservice.asmx"/>
</Services>
</asp:ScriptManager>

Here's a call to the webservice we just wrote

After that, it's written script.

The following are the referenced contents:
<script type= "Text/javascript" >
var numberofemails_original= 0;

var app = Sys.Application;
App.add_init (Applicationinithandler);

function Applicationinithandler (sender, args) {
Inboxservice.getlatestnumberofemails (Oncurrentnumberofemailsready);
}

First, the default is, of course, 0 messages, with variables to hold the current number of messages, followed by a method of invoking Webserice in an initialization event in Ajax, and a callback to the Oncurrentnumberofemailsready method,

The following are the referenced contents:
function Oncurrentnumberofemailsready (result, usercontext, methodname) {
numberofemails_original= result;
Start Checking
Startchecking ();
}

The Oncurrentnumberofemailsready method returns the result of the WebService call (how many letter results in the current state) to the variable, and then calls the Sartchecking () method

function startchecking () {
Inboxservice.getlatestnumberofemails (Onlastestnumberofemailsready);
}

Startchecking method, continue callback Onlastestnumberofemailsready method

function Onlastestnumberofemailsready (result, usercontext, methodname) {
var numberofemails_new= result;
if (Numberofemails_new > Numberofemails_original) {
ShowPopup ();
$get ("Modalbody"). Innerhtml= numberofemails_new-numberofemails_original;

Update The Count here
Numberofemails_original= numberofemails_new;
}
Start Checking again
Window.settimeout (startchecking, 10000);
}

This method, with the current number of messages-the original number of messages, to get how many new messages, and then assign the results to the display area of the modalbody, and remember the current number of messages, variable update OH (numberofemails_original= numberofemails _new;)

Then use Setimeout to set the check every 10000 milliseconds.

The following are the referenced contents:

function ShowPopup () {
$get ("UpdateProgress1"). style.visibility= "Visible";
$get ("UpdateProgress1"). style.display= "Block";
}
function Hidepopup () {
$get ("UpdateProgress1"). style.visibility= "hidden";
$get ("UpdateProgress1"). style.display= "None";
}
</script>



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.