asp.net two ways to prevent page scrolling

Source: Internet
Author: User
Asp.net| page

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

When Page_Load, execute retainscrollposition ()
Page HTML, the body plus id=mybody, you can add this one time, as long as the inherited page will achieve what you want
(quoting others, the original author I forgot, thanks here)

Two

Add an object control to the location you want, let the page refresh and navigate to the position of the control, using the scrollIntoView method.

public void Scrolltoobject (String str_ctl_name,page Page)
{
if (!page. IsStartupScriptRegistered ("Scrollobject"))
{
Page. RegisterStartupScript ("Msgscrollobject", "<script>document.forms" (0). " +str_ctl_name+ ". scrollIntoView ();d ocument.forms (0)." +str_ctl_name+ ". focus (); </script> ");
}
}

Note: Scrolltoobject: Method Name
Str_ctl_name: Control Name



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.