When loadcontrol is used to dynamically load the user control, a problem occurs: when the user control contains a large amount of content (especially the server control), a scroll will appear on the page, when a control is activated, the page is refreshed, and the focus of the window is not on the control just activated.
There are many ways to solve this problem on the Internet. The simplest way is to add intelligent navigation, as shown inCodeAdd the following statement:
Void page_load (Object sender, eventargs E)
{
This. smartnavigation = true;
}
Although this method may solve this problem, it will lead to other problems. Especially when using dynamic loading controls. For example:
Redirect (url1); 1
Dosomething ();
Redirect (url2); 2
Loadcontrol (control1 );
Addcontrol (control1); 3
Dosomething ();
Control. Visual = false;
Loadcontrol (control2 );
Addcontrol (control2); 4
If you select "back", ie links to the status of 1 instead of status 3. That is to say, when using smart navigation, all the processes of loading dynamic pages are regarded as a webpage.
A good method (http://community.csdn.net/Expert/topic/2779/2779374.xml? Temp =. 850094)
1. Add the ID attribute to the body on the ASPX page: <body id = "thebody" ms_positioning = "gridlayout">
2. Reference in the background code (. CS file): using system. text;
3. Add method:
# Region "Preventing Page scrolling"
Private void retainscrollposition ()
{
Stringbuilder savescrollposition = new stringbuilder ();
Stringbuilder setscrollposition = new stringbuilder ();
Registerhiddenfield ("_ scrollpos", "0 ");
Savescrollposition. append ("<script language = 'javascript '> ");
Savescrollposition. append ("function savescrollposition (){");
Savescrollposition. append ("document. Forms [0]. _ scrollpos. value = thebody. scrolltop ;");
Savescrollposition. append ("}");
Savescrollposition. append ("thebody. onscroll = savescrollposition ;");
Savescrollposition. append ("</SCRIPT> ");
Registerstartupscript ("savescroll", savescrollposition. tostring ());
If (page. ispostback)
{
Setscrollposition. append ("<script language = 'javascript '> ");
Setscrollposition. append ("function setscrollposition (){");
Setscrollposition. append ("thebody. scrolltop =" + request ["_ scrollpos"] + ";");
Setscrollposition. append ("}");
Setscrollposition. append ("thebody. onload = setscrollposition ;");
Setscrollposition. append ("</SCRIPT> ");
Registerstartupscript ("setscroll", setscrollposition. tostring ());
}
}
# Endregion
4. Call this method in page_load:
This. retainscrollposition ();