On a non-ajax page, you only need to set MaintainScrollPositionOnPostback = "true" on the page to remember the position of the scroll bar.
However, in ajax, this function does not work correctly. How can we make the div automatically maintain the position of the scroll bar?
First, add the asp: panel control on the page, and then add the following method in the aspx. cs Background:
[C #]
Protected void DoMaintainScrollPositionForAjax (UpdatePanel updatePanel, Panel panel)
{
StringBuilder sb = new StringBuilder ();
Sb. AppendLine (@ "function Panel_SaveScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID + '_ scrollposx'). value = document. getElementById (PanelID). scrollLeft;
Document. getElementById (PanelID + '_ ScrollPosY'). value = document. getElementById (PanelID). scrollTop ;}}
Function Panel_RestoreScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID). scrollLeft = document. getElementById (PanelID + '_ scrollposx'). value;
Document. getElementById (PanelID). scrollTop = document. getElementById (PanelID + '_ ScrollPosY'). value ;}}");
ScriptManager. RegisterClientScriptBlock (updatePanel, panel. GetType (), "PanelScrollFunction", sb. ToString (), true );
HiddenField x = new HiddenField ();
X. ID = panel. ClientID + "_ ScrollPosX ";
X. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (x );
HiddenField y = new HiddenField ();
Y. ID = panel. ClientID + "_ ScrollPosY ";
Y. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (y );
String sScript = "Panel_SaveScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterOnSubmitStatement (updatePanel, panel. GetType (), panel. ID + "_ SavePanelScroll", sScript );
If (Page. IsPostBack)
{
X. Value = Page. Request. Form [x. ClientID];
Y. Value = Page. Request. Form [y. ClientID];
SScript = "Panel_RestoreScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterStartupScript (updatePanel, panel. GetType (), panel. ID + "_ SetPanelScroll", sScript, true );
}
}
Protected void DoMaintainScrollPositionForAjax (UpdatePanel updatePanel, Panel panel)
{
StringBuilder sb = new StringBuilder ();
Sb. AppendLine (@ "function Panel_SaveScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID + '_ scrollposx'). value = document. getElementById (PanelID). scrollLeft;
Document. getElementById (PanelID + '_ ScrollPosY'). value = document. getElementById (PanelID). scrollTop ;}}
Function Panel_RestoreScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID). scrollLeft = document. getElementById (PanelID + '_ scrollposx'). value;
Document. getElementById (PanelID). scrollTop = document. getElementById (PanelID + '_ ScrollPosY'). value ;}}");
ScriptManager. RegisterClientScriptBlock (updatePanel, panel. GetType (), "PanelScrollFunction", sb. ToString (), true );
HiddenField x = new HiddenField ();
X. ID = panel. ClientID + "_ ScrollPosX ";
X. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (x );
HiddenField y = new HiddenField ();
Y. ID = panel. ClientID + "_ ScrollPosY ";
Y. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (y );
String sScript = "Panel_SaveScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterOnSubmitStatement (updatePanel, panel. GetType (), panel. ID + "_ SavePanelScroll", sScript );
If (Page. IsPostBack)
{
X. Value = Page. Request. Form [x. ClientID];
Y. Value = Page. Request. Form [y. ClientID];
SScript = "Panel_RestoreScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterStartupScript (updatePanel, panel. GetType (), panel. ID + "_ SetPanelScroll", sScript, true );
}
}
Call aspx. cs in the following method:
[C #]
Protected void Page_Load (object sender, EventArgs e)
{
DoMaintainScrollPositionForAjax (this. UpdatePanel1, Panel1 );
}
Protected void Page_Load (object sender, EventArgs e)
{
DoMaintainScrollPositionForAjax (this. UpdatePanel1, Panel1 );
}