当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  asp.net 2.0 ajax中实现弹出窗口报警提示

 asp.net 2.0 ajax中实现弹出窗口报警提示

点击次数:43 次 发布日期:2008-11-26 10:59:35 作者:源代码网
源代码网推荐      在 web应用中,比如OA中,经常要用到一些提示,比如EMAIL到达了,就做个象MSN那样的提示框,弹出给用户提示,然后
源代码网推荐  再关闭。在asp.net 2.0的ajax中,这个现在不难做到了,刚好看到老外的一篇文章,讲解到,下面小结之
源代码网推荐  (原文:http://aspalliance.com/1306_How_to_Show_MessengerLike_Popups_Using_AJAX)
源代码网推荐  
源代码网推荐   比如有个数据库表,是存放EMAIL的,当数据库表中的EMAIL一有的时候,就提示用户,首先简单写一个WEBSERVICE如下
源代码网推荐  [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;
源代码网推荐   }
源代码网推荐  }
源代码网推荐  这里要注意要在客户端通过AJAX调用WEBSERICE,要加上[ScriptService]
源代码网推荐  
源代码网推荐  2 在default.aspx中,首先加入一个updateprogress控件,如下
源代码网推荐   <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();">
源代码网推荐   <img alt="Hide Popup" src="http://www.zzchn.com/edu/20080727/App_Themes/Default/images/close_vista.gif" border="0" />
源代码网推荐   </span>
源代码网推荐   </div>
源代码网推荐   <div class="modalBody">
源代码网推荐   You received <strong><span id="modalBody"></span></strong>  Email(s).
源代码网推荐   </div>
源代码网推荐   </div>
源代码网推荐   </ProgressTemplate>
源代码网推荐   </asp:UpdateProgress>
源代码网推荐  
源代码网推荐  这里的关闭X按钮,调用javascript的脚本,等阵再说
源代码网推荐  然后当然要加scriptmanager控件了,如下
源代码网推荐   <asp:ScriptManager ID="ScriptManager1" runat="server">
源代码网推荐   <Services>
源代码网推荐   <asp:ServiceReference Path="~/InboxService.asmx" />
源代码网推荐   </Services>
源代码网推荐   </asp:ScriptManager>
源代码网推荐  这里调用了我们刚才写的webservice
源代码网推荐  之后是写script了
源代码网推荐   <script type="text/javascript">
源代码网推荐   var numberOfEmails_original= 0;
源代码网推荐  
源代码网推荐   var app = Sys.Application;
源代码网推荐   app.add_init(applicationInitHandler);
源代码网推荐  
源代码网推荐   function applicationInitHandler(sender, args) {
源代码网推荐   InboxService.GetLatestNumberOfEmails(OnCurrentNumberOfEmailsReady);
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  首先,默认的当然是0封邮件了,有变量来存放当前邮件数量,
源代码网推荐  之后是在ajax中的初始化事件中调用webserice的方法了,并且回调OnCurrentNumberOfEmailsReady方法,
源代码网推荐  
源代码网推荐   function OnCurrentNumberOfEmailsReady(result, userContext, methodName) {
源代码网推荐   numberOfEmails_original= result;
源代码网推荐  
源代码网推荐   // Start Checking
源代码网推荐   StartChecking();
源代码网推荐   }
源代码网推荐  
源代码网推荐  OnCurrentNumberOfEmailsReady方法将WEBSERVICE调用的结果(当前状态下有多少封信RESULT)返回给变量,然后调用sartchecking()方法
源代码网推荐  
源代码网推荐   function StartChecking() {
源代码网推荐   InboxService.GetLatestNumberOfEmails(OnLastestNumberOfEmailsReady);
源代码网推荐   }
源代码网推荐  
源代码网推荐  startchecking方法,继续回调OnLastestNumberOfEmailsReady方法
源代码网推荐  
源代码网推荐   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);
源代码网推荐   }
源代码网推荐  
源代码网推荐  这个方法,用当前邮件数-原来邮件数,就得出新增了多少封邮件了,再将结果赋值给显示区域的modalbody,并且记得把当前邮件数量的
源代码网推荐  变量更新哦(numberOfEmails_original= numberOfEmails_new;
源代码网推荐  )
源代码网推荐  
源代码网推荐  然后再用setimeout来设置每隔10000毫秒检查一次了
源代码网推荐  
源代码网推荐   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>
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华