ASP.Net 2.0 YUI Style UpdateProgress

  • 55679
  • 0
  • AJAX
  • 2008-03-23

ASP.Net 2.0 YUI Style UpdateProgress

配合 UpdatePanel 與 UpdateProgress 控制項,在長時間更新的時候,在瀏覽器中央會出現 YUI Style AJAX Progress Indicator,告知使用者畫面更新中。

參考: YUI Style AJAX Progress Indicator



原本懶惰的做法會出現一些問題,就是頁面任何一個 postback 的動作都會讓 UpdateProgress 被觸發,所以改成下面那個版本來過濾觸發的程序。

長與寬現在是用 121 與 35 常數來取代,是因為怎麼抓都抓不到 Panel 的 width 與 height,只好自己用 printscreen 來看實際長寬在各除以 2 而得。

修正後懶惰的做法:

  1. <script type="text/javascript" language="javascript">
  2. var postBackElementID;
  3. Sys.WebForms.PageRequestManager.getInstance ( ).add_beginRequest ( function (sender, args)
  4. {
  5. postBackElementID = args.get_postBackElement ( ).id.substring (args.get_postBackElement ( ).id.lastIndexOf ( '_' ) + 1 );
  6. if (postBackElementID == 'Button' )
  7. {
  8. $get( '<%= UpdateProgress1.ClientID %>' ).style.display = '';
  9. var pnlPopup = $get( '<%= UpdateProgress1.Controls[0].Controls[1].ClientID %>' );
  10. pnlPopup.style.display = '';
  11. var width = (document.all ) ? document.documentElement.clientWidth : window.innerWidth;
  12. var height = (document.all ) ? document.documentElement.clientHeight : window.innerHeight;
  13. var x = Math.round (width / 2 ) - 121;
  14. var y = Math.round (height / 2 ) - 35;
  15. Sys.UI.DomElement.setLocation (pnlPopup, x, y);
  16. }
  17. } );
  18. Sys.WebForms.PageRequestManager.getInstance ( ).add_endRequest ( function (sender, args)
  19. {
  20. if (postBackElementID == 'Button' )
  21. {
  22. $get( '<%= UpdateProgress1.ClientID %>' ).style.display = 'none';
  23. var pnlPopup = $get( '<%= UpdateProgress1.Controls[0].Controls[1].ClientID %>' );
  24. pnlPopup.style.display = 'none';
  25. }
  26. } );
  27. </script>



UpdateProgress

  1. <asp:UpdateProgress ID="UpdateProgress1" runat="server" >
  2. <ProgressTemplate>
  3. <asp:Panel ID="pnlPopup" runat="server" CssClass="progress" style = "display: none;" >
  4. <div class="Container" style="background: url(Images/sprite.png) repeat-x 0px 0px;" >
  5. <div class="Header" >
  6. 資料載入中, 請耐心等待... </div>
  7. <div class="Body" >
  8. <asp:Image ID="ImageProgress" runat="server" ImageUrl="Images/activity.gif" />
  9. </div>
  10. </div>
  11. </asp:Panel>
  12. </ProgressTemplate>
  13. </asp:UpdateProgress>