ASP.NET編程之實現(xiàn)彈窗報警提示淺析
ASP.NET編程之實現(xiàn)彈窗報警提示的前言,在web應(yīng)用中,比如OA中,經(jīng)常要用到一些提示,比如EMAIL到達了,就做個象MSN那樣的提示框,彈出給用戶提示,然后再關(guān)閉。在asp.net 2.0的ajax中,這個現(xiàn)在不難做到了,剛好看到老外的一篇文章,講解到,下面小結(jié)
比如有個數(shù)據(jù)庫表,是存放EMAIL的,當數(shù)據(jù)庫表中的EMAIL一有的時候,就提示用戶,首先簡單寫一個WEBSERVICE如下
以下為引用的內(nèi)容:
- [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調(diào)用WEBSERICE,要加上[ScriptService]
ASP.NET編程之實現(xiàn)彈窗報警提示在default.aspx中,首先加入一個updateprogress控件,如下
以下為引用的內(nèi)容:
- ﹤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="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﹥
這里的關(guān)閉X按鈕,調(diào)用javascript的腳本,等陣再說
然后當然要加scriptmanager控件了,如下
以下為引用的內(nèi)容:
- ﹤asp:ScriptManager ID="ScriptManager1" runat="server"﹥
- ﹤Services﹥
- ﹤asp:ServiceReference Path="~/InboxService.asmx" /﹥
- ﹤/Services﹥
- ﹤/asp:ScriptManager﹥
這里調(diào)用了我們剛才寫的webservice
ASP.NET編程之實現(xiàn)彈窗報警提示:Script
以下為引用的內(nèi)容:
- ﹤script type="text/javascript"﹥
- var numberOfEmails_original= 0;
- var app = Sys.Application;
- app.add_init(applicationInitHandler);
- function applicationInitHandler(sender, args) {
- InboxService.GetLatestNumberOfEmails(OnCurrentNumberOfEmailsReady);
- }
首先,默認的當然是0封郵件了,有變量來存放當前郵件數(shù)量,之后是在ajax中的初始化事件中調(diào)用webserice的方法了,并且回調(diào)OnCurrentNumberOfEmailsReady方法,
以下為引用的內(nèi)容:
- function OnCurrentNumberOfEmailsReady(result, userContext, methodName) {
- numberOfEmails_original= result;
- // Start Checking
- StartChecking();
- }
- OnCurrentNumberOfEmailsReady方法將WEBSERVICE調(diào)用的結(jié)果(當前狀態(tài)下有多少封信RESULT)返回給變量,然后調(diào)用sartchecking()方法
- function StartChecking() {
- InboxService.GetLatestNumberOfEmails(OnLastestNumberOfEmailsReady);
- }
- startchecking方法,繼續(xù)回調(diào)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);
- }
這個方法,用當前郵件數(shù)-原來郵件數(shù),就得出新增了多少封郵件了,再將結(jié)果賦值給顯示區(qū)域的modalbody,并且記得把當前郵件數(shù)量的,變量更新哦(numberOfEmails_original= numberOfEmails_new;)
然后再用setimeout來設(shè)置每隔10000毫秒檢查一次了
以下為引用的內(nèi)容:
- 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﹥
ASP.NET編程之實現(xiàn)彈窗報警提示的相關(guān)信息就向你介紹到這里,希望對你有所幫助。
【編輯推薦】