locked
Timer Control and Postback RRS feed

  • Question

  • User1245007159 posted

    I have a timer control in  my page placed inside update panel. Its refreshes every second. When I scroll down page, Page is moving up,as page is refreshing every second. Whole page is refreshing.

    I tried updatemode=conditional,but no use.

    Can anyone help?

    Tuesday, October 20, 2015 2:30 PM

All replies

  • User2103319870 posted

    You can try adding MaintainScrollPositionOnPostback="true" in the page directive.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="YourPage.aspx.cs" Inherits="Sample.YourPage" MaintainScrollPositionOnPostback="true" %>

     Refer the below link for more details

    Tuesday, October 20, 2015 2:52 PM
  • User2103319870 posted

    An Alternative approach is to use the below code to persist the scroll postiion on partial postback.  for this You have to wrap all your controls inside a div block instead of panel control and then use the below code

     <script type="text/javascript">
            //Ensure that you place the Javascript code after the script manager
            var xPos, yPos;
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_beginRequest(BeginRequestHandler);
            prm.add_endRequest(EndRequestHandler);
            function BeginRequestHandler(sender, args) {
                xPos = $get('dvScroll').scrollLeft;
                yPos = $get('dvScroll').scrollTop;
            }
            function EndRequestHandler(sender, args) {
                $get('dvScroll').scrollLeft = xPos;
                $get('dvScroll').scrollTop = yPos;
            }
        </script>

    Complete Code:

    Complete Code:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <script type="text/javascript">
            //Ensure that you place the Javascript code after the script manager
            var xPos, yPos;
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_beginRequest(BeginRequestHandler);
            prm.add_endRequest(EndRequestHandler);
            function BeginRequestHandler(sender, args) {
                xPos = $get('dvScroll').scrollLeft;
                yPos = $get('dvScroll').scrollTop;
            }
            function EndRequestHandler(sender, args) {
                $get('dvScroll').scrollLeft = xPos;
                $get('dvScroll').scrollTop = yPos;
            }
        </script>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <div id="dvScroll" style="overflow-y: scroll; height: 400px;">
                    <%-- Add all your controls here and change the height of div as per your wish
    You can add your GridView control here
    --%> </div>
    <asp:Button ID="Button1" runat="server" Text="Postback" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" /> </Triggers> </asp:UpdatePanel> </form> </body> </html>

    Source URL : Maintain Scroll Position after Asynchronous Postback

    Demo

    Tuesday, October 20, 2015 2:54 PM
  • User1245007159 posted

    a2h,Thanks for answer.

    In my case, i dont have a button. All I have is simple timer control which runs every second based on current time. I tried your javascript code. if I use, javascript, timer will not run.

    <asp:Timer ID="Timer2" runat="server" Interval="1"></asp:Timer>
    
    <asp:Label ID="Label2" runat="server" Text="Current Time: " Font-Bold="true"></asp:Label>
           
    <asp:Label ID="Label1" runat="server" Font-Bold="true" Font-Size="Medium"></asp:Label>
    
       </ContentTemplate>
    
    </asp:UpdatePanel>
    

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            
                Label1.Text = datetime.Now.ToLongTimeString()
    
     End Sub




    Wednesday, October 21, 2015 12:25 AM
  • User61956409 posted

    Hi vinaybmm,

    Firstly, have you tried to add MaintainScrollPositionOnPostback="true" in the page directive?

    Secondy, you could also store the scroll position in a hidden field, then you could retrieve the value form it and reset the scroll position based on the HiddenField value. The following articles are for your reference.

    http://www.aspsnippets.com/Articles/Maintain-Scroll-Position-of-DIV-on-PostBack-in-ASPNet.aspx

    http://www.aspsnippets.com/Articles/ASPNet-MaintainScrollPositionOnPostback-not-working-in-Firefox-and-Chrome.aspx

    Best Regards,

    Fei Han

    Wednesday, October 21, 2015 2:49 AM
  • User1245007159 posted

    Thanks Fei Han for answer.

     MaintainScrollPositionOnPostback="true" is not working for me.

    The links you provided also did not help me.

    Friday, October 23, 2015 4:10 AM
  • User61956409 posted

    Hi vinaybmm,

    It seems that you also tried Javascript code, please debug the code using F12 developer tools to check if it causes any error.

    Best Regards,

    Fei Han

    Monday, November 2, 2015 3:48 AM