How to locate the control after the control is dynamically loaded in Asp.net

Source: Internet
Author: User

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.