asp.net via Ajax UpdatePanel back after the scrollbar position change solution

  • 2020-05-10 17:57:38
  • OfStack

The main methods are:

Save the current scorll value with a hidden control. Reset scroll according to the value of scroll.


1. First, save the scorll value with the onscroll event, and record the scroll value with HiddenField
 
<div id="lv_content" class="unify_content" style="padding-left: 0; height: 455px;" onscroll="SetScrollPosition(this)"> 
</div> 
<asp:HiddenField ID="HiddenFieldScroll" runat="server" /> 



code
//2, write onscroll event, mainly used to save the scroll value of the current control
 
function funSaveScroll(sender) { 
// Get the hidden control  
var vScroll = $get('ctl00_ContentPlaceHolder1_hfLvScroll'); 
if (vScroll != null) { 
// Sets the value of the hidden control to scorll value  
vScroll.value = sender.scrollTop; 
} 
} 



code
 
//3 , processed in the page's load completion event scorll value  
var prm = Sys.WebForms.PageRequestManager.getInstance(); 
// Set the load completion event  
prm.add_pageLoaded(pageLoaded); 

// Load finished resetting the control scroll value  
function pageLoaded(sender, args) { 
// Get to set scroll The value of control  
var vDiv = $get("lv_content"); 
if (vDiv != null) { 
// Get the hidden control  
var vScrollValue = $get("ctl00_ContentPlaceHolder1_hfLvScroll"); 
// Set control scroll value  
vDiv.scrollTop = vScrollValue.value; 
} 
} 

The above code can be used to solve the problem of scroll changes after the return.

If you are using the Master page, you can place the above code in the master page for public use.

1) add HiddenField to the Master page first.

< asp:HiddenField ID="HiddenFieldScroll" runat="server" / >



code
 
/* 
2) in master On the page onscroll Event that records controls in other pages scroll value  
 Method of use : Simply set it in the control you want to set ID And add onscroll Events are ok. Namely to join  onscroll="SetScrollPosition(this)" statements  
*/ 
function SetScrollPosition(sender) { 
var scrollTemp = $get("ctl00_HiddenFieldScroll"); 
if (scrollTemp != null) { 
// To set the control ID and scroll value 1 The save. In the "|" separated  
scrollTemp.value = sender.id + "|" + sender.scrollTop; 
} 
} 





code
 
//3) Handled in the page's load completion event scorll value  
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(onPageLoaded); 
//The Pageloaded for scroll orientation 
function onPageLoaded(sender, args) { 
var scrollTemp = $get("ctl00_HiddenFieldScroll"); 
if (scrollTemp != null) { 
var scorllValue = scrollTemp.value; 
if (scorllValue.length > 0) { 
var target = scorllValue.split("|"); 
if (target.length > 1) { 
var targetControlID = target[0]; 
var targetValue = target[1]; 
var targetControl = $get(targetControlID); 
// Gets the control to set  
if (targetControl == null || targetValue.length < 1) { 
return; 
} 
// Set control scroll value  
targetControl.scrollTop = targetValue; 
} 
} 
} 
} 


4) when called, add onscroll="SetScrollPosition(this)" in other page controls and set ID
< div id="lv_content" class="unify_content" style="padding-left: 0; height: 455px;" onscroll="SetScrollPosition(this)" >
< /div >

Related articles: