Asp.net two methods to prevent Page scrolling [Reference]

Source: Internet
Author: User

I,
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 = MyBody. scrollTop ;");
SaveScrollPosition. Append ("}");
SaveScrollPosition. Append ("MyBody. onscroll = saveScrollPosition ;");
SaveScrollPosition. Append ("</script> ");

RegisterStartupScript ("saveScroll", saveScrollPosition. ToString ());

If (Page. IsPostBack)
{
SetScrollPosition. Append ("<script language = 'javascript '> ");
SetScrollPosition. Append ("function setScrollPosition (){");
SetScrollPosition. Append ("MyBody. scrollTop =" + Request ["_ SCROLLPOS"] + ";");
SetScrollPosition. Append ("}");
SetScrollPosition. Append ("MyBody. onload = setScrollPosition ;");
SetScrollPosition. Append ("</script> ");

RegisterStartupScript ("setScroll", setScrollPosition. ToString ());
}
}

Execute RetainScrollPosition () during page_load ()
In the page html, add id = MyBody to the body. As long as the inherited page is implemented
(I forgot to cite others. Thank you here)

II,

Add an object control at the location you need to refresh the page and locate the control location. The scrollIntoView method is used.

Public void ScrollToObject (string str_Ctl_Name, Page page)
{
If (! Page. IsStartupScriptRegistered ("scrolobject "))
{
Page. registerStartupScript ("msgscrolobject", "<script> document. forms (0 ). "+ str_Ctl_Name + ". scrollIntoView (); document. forms (0 ). "+ str_Ctl_Name + ". focus (); </script> ");
}
}

Note: ScrollToObject: Method Name
Str_Ctl_Name: Control name
Example: ScrollToObject (Button1, Page );

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.