Method 1: "Prevent Page scrolling" # Region "Prevent Page scrolling"
Public Void Retainscrollposition (system. Web. UI. Page mypage, String Strbodyname)
{
Stringbuilder savescrollposition = New Stringbuilder ();
Stringbuilder setscrollposition = New Stringbuilder ();
Mypage. registerhiddenfield ( " _ Scrollpos " , " 0 " );
Savescrollposition. append ( " <Script language = 'javascript '> " );
Savescrollposition. append ( " Function savescrollposition (){ " );
Savescrollposition. append ( " Document. Forms [0]. _ scrollpos. value = " + Strbodyname + " . Scrolltop; " );
Savescrollposition. append ( " } " );
Savescrollposition. append (strbodyname + " . Onscroll = savescrollposition; " );
Savescrollposition. append ( " </SCRIPT> " );
Mypage. registerstartupscript ( " Savescroll " , Savescrollposition. tostring ());
If (Mypage. ispostback)
{
Setscrollposition. append ( " <Script language = 'javascript '> " );
Setscrollposition. append ( " Function setscrollposition (){ " );
Setscrollposition. append ( " " + Strbodyname + " . Scrolltop = " + Mypage. request [ " _ Scrollpos " ] + " ; " );
Setscrollposition. append ( " } " );
Setscrollposition. append (strbodyname + " . Onload = setscrollposition; " );
Setscrollposition. append ( " </SCRIPT> " );
Mypage. registerstartupscript ( " Setscroll " , Setscrollposition. tostring ());
}
}
# Endregion
Method 2:
Page. smartnavigation = true;
It may cause JavaScript errors on your page. This is a bug in ASP. NET.
method 3 :
using the principle of , I simplified the process and divided it into three parts:
1.
insert between
and , used to record the current scroll position of the page.
2.
name , for example, , write the onscroll event of the body as follows:
3.
Add the following Code (VB.net) to the page_load event ), when IE displays the page, it immediately locates the original scroll position:
dim JS as string
JS = ""
response. write (JS) Not here, we will share the solution of not in a project.
The idea of not is to write a segment script after the event is executed, so that the page will automatically scroll to the control place before the page is refreshed, reducing the inconvenience caused by PAGE refreshing.
For example, if you press a button, the script automatically scrolls the page to the button position.
A scripthelper class is not used to write a segment script. This class has a getviewcontrolscript (string controlname) method, which returns a client script, the input parameter is the ID of the control.
Scripthelper class code:
/// < Summary >
/// Provides some methods to generate page scripts
/// </ Summary >
Public class scripthelper
{
/// < Summary >
/// Obtain the script for the client to view the control
/// </ Summary >
/// < Param Name = "Controlname" > </ Param >
/// < Returns > Script code </ Returns >
Public static string getviewcontrolscript (string controlname)
{
// Create the client function viewobj
String script = "\ n ";
Script + =" < Script Language = \ "Javascript \" > \ N " ;
Script + = " Function Viewobj (objname) \ n " ;
Script + = " {\ N " ;
Script + = " VaR OBJ = Document. All. Item (objname); \ n " ;
Script + = " If (OBJ ! = Null ) \ N " ;
Script + = " {\ N " ;
Script + = " \ Tobj. scrollintoview (); \ n " ;
Script + = " \ Tobj. Focus (); \ n " ;
Script + = " } \ N " ;
Script + = " } \ N " ;
// Create the client function todo
Script + = " Function Todo () " ;
Script + = " {\ N " ;
Script + = string. Format ( " SetTimeout (\ " Viewobj ('{0 }')\ " , 1000 ); \ N " , Controlname );
Script + = " } \ N " ;
Script + = " Window. onload = Todo; \ n " ;
Script + = " </ Script > \ N ";
Return script;
}
}
Example:
To facilitate script input, I put a label: lblscript on the page, and set the enableviewstate attribute and visible attribute of lblscript to false.
Then add the script input code after the operation code of the click event of lblscrpt, as shown below:
Private void btnsave_click (Object sender, system. eventargs E)
{
Project. updateprojectinfo (DS );
Lblscript. Text = scripthelper. getviewcontrolscript ("btnsave ");
}
After clicking the btnsave button, the page will automatically scroll to the btnsave location, reducing the inconvenience caused by PAGE refresh.