Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to keep scroll position in Asp.net?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.44k
    Comment on it

    In asp.net, there are different ways to retain scroll position on postback. After Postback vertical scrollbar leave its position and access the same position on the page so to resolve this problem asp.net gives a property: “MaintainScrollPositionOnPostBack” which maintains the scrollbar position on the webpage.

    There are three methods to implement this property:

    Method1: If we want to implement scroll position programmatically

    we can add this property on page load event. Also, we can add the same on button click event as:

    For Example:

    Page.MaintainScrollPositionOnPostBack = true;

     

    Method2: You have to mention directive in page directive section of aspx design page. It can be placed on the pages individually wherever the user wants to maintain scroll position.

    For example:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  
             MaintainScrollPositionOnPostback="true"  Inherits="_Default" %>

     

    Method3: If the user wants to implement this functionality for every page in the project, then this property will be added in Web.config file of the project. To maintain scroll position,we add this property in the page section of web.config file.

    For example:

    <configuration>
       <system.web>
          <pages maintainScrollPositionOnPostBack="true" />
       </system.web>
    </configuration>

    Include the following code in your design part(Aspx page)

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ScrollWebForm.aspx.cs" Inherits="WebApplication8.ScrollWebForm"%>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    
    </head>
    <body>
        <form id="form1" runat="server">
     <b>Please Scroll Down and click on the Submit button</b>
            <br /><br /><br /> <br />
        1.  This is a scrollPosition testing Page
        <br />
        2. This is a scrollPosition testing Page
        <br />
        3.  This is a scrollPosition testing Page
        <br />
        4.  This is a scrollPosition testing Page
        <br />
        5.  This is a scrollPosition testing Page
        <br />
        6.  This is a scrollPosition testing Page
        <br />
        7. This is a scrollPosition testing Page
        <br />
        8.  This is a scrollPosition testing Page
        <br />
        9.  This is a scrollPosition testing Page
        <br />
        10.  This is a scrollPosition testing Page
        <br />
        11.  This is a scrollPosition testing Page
        <br />
        12.  This is a scrollPosition testing Page
        <br />
        13. This is a scrollPosition testing Page
        <br />
        14.  This is a scrollPosition testing Page
        <br />
        15. This is a scrollPosition testing Page
        <br />
        16.  This is a scrollPosition testing Page
        <br />
        17.  This is a scrollPosition testing Page
        <br />
        18.  This is a scrollPosition testing Page
        <br />
        19.  This is a scrollPosition testing Page
        <br />
        20.  This is a scrollPosition testing Page
        <br />
        21.  This is a scrollPosition testing Page
        <br />
        22.  This is a scrollPosition testing Page
        <br />
        23.  This is a scrollPosition testing Page
        <br />
        24.  This is a scrollPosition testing Page
        <br />
        25.  This is a scrollPosition testing Page
        <br /><br /><br /> <br /><br /><br /><br />
        <br /><br /><br /> <br /><br /><br /><br />
        <br /><br /><br /> <br /><br /><br /><br />
         
      <asp:Button Text="Submit" runat="server" OnClick="btnScroll_Click" />
    <br /><br /><br /><br /><br /> <br /><br /><br /><br />
    </form>
    </body>
    </html>

     

    Include the following code in code behind page(aspx.cs) if you want to implement this functionality programmatically:

    protected void btnScroll_Click(object sender, EventArgs e)
            {
                Page.MaintainScrollPositionOnPostBack = true;
            }

    See the below screenshot for the reference:

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: