Analysis of the pop-up window alarm prompt for ASP. NET Programming

Source: Internet
Author: User

ASP. NET Programming implementation pop-up window warning prompt preface, in web applications, such as OA, often need to use some prompts, such as e-mail arrived, just make a prompt box like MSN, A prompt is displayed for the user and then closed. In asp.net 2.0's ajax, This is not hard to do now. I just saw an article from a foreigner and explained it. The following is a summary.

For example, if there is a database table that stores emails, when there is an EMAIL in the database table, the user will be prompted. First, simply write a WEBSERVICE as follows:

Reference content is as follows:

 
 
  1. [ScriptService]  
  2. public class InboxService : System.Web.Services.WebService  
  3. {  
  4. [WebMethod]  
  5. public int GetLatestNumberOfEmails()  
  6. {  
  7. int numberOfEmails = 0;  
  8. using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings[0].ConnectionString))  
  9. {  
  10. using (SqlCommand cmd = new SqlCommand("GetLatestNumberOfEmails", conn))  
  11. {  
  12. cmd.CommandType = CommandType.StoredProcedure;  
  13. conn.Open();  
  14. numberOfEmails = (int)cmd.ExecuteScalar();  
  15. }  
  16. }  
  17. return numberOfEmails;  
  18. }  

Note that you need to add [ScriptService] to the client to call WEBSERICE through AJAX.

ASP. NET Programming implementation pop-up window alarm prompt in default. aspx, first add an updateprogress control, as shown below

Reference content is as follows:

 
 
  1. ﹤asp:UpdateProgress DynamicLayout="False" ID="UpdateProgress1" runat="server"﹥  
  2. ﹤ProgressTemplate﹥  
  3. ﹤div id="modal" class="modal"﹥  
  4. ﹤div class="modalTop"﹥  
  5. ﹤div class="modalTitle"﹥My Inbox﹤/div﹥  
  6. ﹤span style="CURSOR: hand" onclick="javascript:HidePopup();"﹥  
  7. ﹤img alt="Hide Popup" src="App_Themes/Default/images/close_vista.gif" border="0" /﹥  
  8. ﹤/span﹥  
  9. ﹤/div﹥  
  10. ﹤div class="modalBody"﹥  
  11. You received ﹤strong﹥﹤span id="modalBody"﹥﹤/span﹥﹤/strong﹥  Email(s).  
  12. ﹤/div﹥  
  13. ﹤/div﹥  
  14. ﹤/ProgressTemplate﹥  
  15. ﹤/asp:UpdateProgress﹥ 

Close the X button here and call the javascript script.

Then, add the scriptmanager control as follows:

Reference content is as follows:

 
 
  1. ﹤asp:ScriptManager ID="ScriptManager1" runat="server"﹥  
  2. ﹤Services﹥  
  3. ﹤asp:ServiceReference Path="~/InboxService.asmx" /﹥  
  4. ﹤/Services﹥  
  5. ﹤/asp:ScriptManager﹥ 

The webservice we just wrote is called here.

ASP. NET Programming implementation pop-up window alarm prompt: Script

Reference content is as follows:

 
 
  1. ﹤script type="text/javascript"﹥  
  2. var numberOfEmails_original= 0;  
  3.  
  4. var app = Sys.Application;  
  5. app.add_init(applicationInitHandler);  
  6.  
  7. function applicationInitHandler(sender, args) {  
  8. InboxService.GetLatestNumberOfEmails(OnCurrentNumberOfEmailsReady);  
  9. }  

First of all, the default mail is of course 0. there are variables to store the current mail quantity. Then, the webserice method is called in the ajax initialization event and the OnCurrentNumberOfEmailsReady method is called back,

Reference content is as follows:

 
 
  1. FunctionOnCurrentNumberOfEmailsReady (result, userContext, methodName ){
  2. NumberOfEmails_original = result;
  3. // Start Checking 
  4. StartChecking ();
  5. }
  6. OnCurrentNumberOfEmailsReady method returns the RESULT of the WEBSERVICE call to the variable, and then calls the sartchecking () method.
  7.  
  8. FunctionStartChecking (){
  9. InboxService. GetLatestNumberOfEmails (OnLastestNumberOfEmailsReady );
  10. }
  11.  
  12. Startchecking method, continue to call back the OnLastestNumberOfEmailsReady Method
  13.  
  14. FunctionOnLastestNumberOfEmailsReady (result, userContext, methodName ){
  15. VarNumberOfEmails_new = result;
  16. If(NumberOfEmails_new> numberOfEmails_original ){
  17. ShowPopup ();
  18. $ Get ("ModalBody"). InnerHTML = numberOfEmails_new-numberOfEmails_original;
  19.  
  20. // Update the count here 
  21. NumberOfEmails_original = numberOfEmails_new;
  22. }
  23. // Start checking again 
  24. Window. setTimeout (startcheck, 10000 );
  25. }

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.

Reference content is as follows:

 
 
  1. function ShowPopup() {  
  2. $get("UpdateProgress1").style.visibility= "visible";  
  3. $get("UpdateProgress1").style.display= "block";  
  4. }  
  5. function HidePopup() {  
  6. $get("UpdateProgress1").style.visibility= "hidden";  
  7. $get("UpdateProgress1").style.display= "none";  
  8. }  
  9. ﹤/script﹥ 

The information about the implementation of the pop-up window alarm prompt in ASP. NET programming will be introduced to you here, hoping to help you.

  1. Analysis of JavaScript insertion methods in ASP. NET
  2. Analysis of ASP. NET JavaScript and ole db Design network diary
  3. Code Analysis of Cookie deletion in ASP. NET
  4. Syntax analysis of nested If statements in ASP. NET Programming
  5. Analysis of Date and Time Processing in ASP. NET Programming

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.