Enable pop-up alarms in ASP. NET 2.0

Source: Internet
Author: User
Tags call back
In Web applications, for example, OA, some prompts are often used. For example, if an email arrives, a prompt box like MSN is displayed, prompting the user, and then disabling it. In Asp.net 2.0 Ajax, This is not hard to do now. I just saw an article from a foreigner and explained it. Below is a summary, for example, there is a database table that stores emails, when there is an email in the database table, you are prompted to write a WebService as follows:
[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;
}
}
Note that you need to add [scriptservice] to the client to call webserice through Ajax.
2 In default. aspx, first add an updateprogress control, as shown below:
<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">

</Span>
</Div>
<Div class = "modalbody">
You have Ed <strong> <span id = "modalbody"> </span> </strong> email (s ).
</Div>
</Div>
</Progresstemplate>
</ASP: updateprogress>
Close the X button here and call the Javascript script.
Then, add the scriptmanager control as follows:
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Services>
<Asp: servicereference Path = "~ /Inboxservice. asmx "/>
</Services>
</ASP: scriptmanager>
The WebService we just wrote is called here.
The script is written later.
<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 mail is of course 0, and there is a variable to store the current mail quantity,
The webserice method is called in the Ajax initialization event and the oncurrentnumberofemailsready method is called back,
Function oncurrentnumberofemailsready (result, usercontext, methodname ){
Numberofemails_original = result;
// Start checking
Startchecking ();
}
Oncurrentnumberofemailsready method returns the result of WebService call (number of messages in the current status) to the variable, and then calls the sartchecking () method
Function startchecking (){
Inboxservice. getlatestnumberofemails (onlastestnumberofemailsready );
}
Startchecking method, continue to call back the 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 (startcheck, 10000 );
}
This method uses the current number of mails-the original number of mails to determine how many new mails are added. Then, assign the result to modalbody in the display area and remember to set the current number of mails, variable Update (numberofemails_original = numberofemails_new ;)
Then we use setimeout to set the check interval to 10000 milliseconds.
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>
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.