ASP.NET 2.0中實現彈窗報警提示_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:ASP.NET十分有用的頁面間傳值方法
      一、目前在ASP.NET中頁面傳值共有這么幾種方式: 1、表單提交 <form action= "target.aspx" method = "post" name = "form1"> <input name = "

      在 web應用中,比如OA中,經常要用到一些提示,比如EMAIL到達了,就做個象MSN那樣的提示框,彈出給用戶提示,然后再關閉。在asp.net 2.0的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="App_Themes/Default/images/close_vista.gif" border="0" />
      </span>
      </div>
      <div class="modalBody">
      You received <strong><span id="modalBody"></span></strong>&nbsp; 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>

      分享:ASP.NET生成靜態網頁的方法
      環境:Microsoft .NET Framework SDK v1.1 OS:Windows Server 2003 中文版 ASP.Net生成靜態HTML頁,在Asp中實現的生成靜態頁用到的FileSystemObject對象,在.Net中涉及此類操作的是System.IO

      來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
      相關.Net教程