2014-02-06 13 views
0

我有一個自定義ASP.NET控件,下拉/樹視圖,需要添加Begin(End-)RequestHandlers以防止在UpdatePanel部分回發期間滾動到容器頂部(如所述here)。他們是動態添加像這樣:當我控件添加到頁面的多個實例開始/ EndRequestHandler自定義ASP.NET控件中的多個處理程序

Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, string.Format(@" 
        var xPos, yPos; 
        var prm = Sys.WebForms.PageRequestManager.getInstance(); 

        function BeginRequestHandler(sender, args) {{ 
        if ($get('{0}') != null) {{ 
         // Get X and Y positions of scrollbar before the partial postback 
         xPos = $get('{0}').scrollLeft; 
         yPos = $get('{0}').scrollTop; 
        }} 
       }} 

       function EndRequestHandler(sender, args) {{ 
        if ($get('{0}') != null) {{ 
         // Set X and Y positions back to the scrollbar 
         // after partial postback 
         $get('{0}').scrollLeft = xPos; 
         $get('{0}').scrollTop = yPos; 
        }} 
       }} 

       prm.add_beginRequest(BeginRequestHandler); 
       prm.add_endRequest(EndRequestHandler); ", this.ItemsContainer.ClientID), true); 

的問題開始。這兩個腳本都呈現並註冊,但最後只有一個,最後一個頁面,最終附加到容器,即。只要面板X或Y有更新,只有面板Y的JS被執行 - 兩次!

一個更好的選項肯定會追加一對Begin/End處理程序,我可以執行它,例如,爲RegisterStartupScript方法添加一個靜態鍵。唯一的問題是我需要傳遞當前更新的UpdatePanel面板的參數。

任何想法如何做到這一點,並結合上述?

回答

1

此修改自動恢復所有面板的滾動位置。

<script type="text/javascript"> 
    (function() { 
     var scrolledDivs = []; 
     var prm = Sys.WebForms.PageRequestManager.getInstance(); 

     prm.add_beginRequest(function (sender, args) { 

      //store the scroll positions of all "scrolled" div elements 
      //UpdatePanel and Panel both are div elements 
      scrolledDivs = []; 

      $('div').each(function() { 
       var div = $(this); 
       if (div.scrollLeft() != 0 || div.scrollTop() != 0) { 
        scrolledDivs.push({ 
         element: this, 
         scrollLeft: div.scrollLeft(), 
         scrollTop: div.scrollTop() 
        }); 
       } 
      }); 
     }); 

     prm.add_endRequest(function (sender, args) { 
      //restore scroll positions 
      $.each(scrolledDivs, function (index, value) { 
       $(value.element).scrollLeft(value.scrollLeft).scrollTop(value.scrollTop); 
      }); 
     }); 
    })(); 
</script> 

注意:您需要包括JQuery的

+0

這似乎是正常工作,但發生了什麼(可能與代碼本身無關)是,在附加兩個處理程序後,第一個異步回傳是好的,下一個返回'未知錯誤'。在附加調試器後,在Sys.Net.XMLHttpExecutor - > this._onReadyStateChange [...]中的4723行周圍的MicrosoftAjax.debug.js中引發未知異常。你有什麼線索可能發生?我已將VS附加到C#代碼,並且在那裏拋出異常。 – n0e

+0

好吧,看起來如果只附加其中一個處理程序,無論是Begin ..還是End ..,都不會拋出異常。以某種方式將兩個結果附加到異常並打破AJAX功能。 – n0e

+0

使用其他瀏覽器時能否發佈錯誤? Firefox或chrome – LostInComputer

1

使用從LostInComputer一點修改後的代碼的解決方案是這樣的:

  Page.ClientScript.RegisterStartupScript(this.GetType(), "scrollRestorer", @" 
        var scrolledDivs = []; 
        var prm = Sys.WebForms.PageRequestManager.getInstance(); 

        function BeginRequestHandler(sender, args) { 
        //store the scroll positions of all 'scrolled' div elements 
        //UpdatePanel and Panel both are div elements 
        scrolledDivs = []; 

        $('div').each(function() { 
         var div = $(this); 
         if (div.scrollLeft() != 0 || div.scrollTop() != 0) { 
          scrolledDivs.push({ 
           element: this.id, 
           scrollLeft: div.scrollLeft(), 
           scrollTop: div.scrollTop() 
          }); 
         } 
        }); 
       } 

       function EndRequestHandler(sender, args) { 
        //restore scroll positions 
        $.each(scrolledDivs, function (index, value) { 
         $('#' + value.element).scrollLeft(value.scrollLeft).scrollTop(value.scrollTop); 
        }); 
       } 

       prm.add_beginRequest(BeginRequestHandler); 
       prm.add_endRequest(EndRequestHandler); ", true); 

項目本身不能存儲在陣列中,唯一ID應該存儲。

相關問題