locked
codebehind scroll in page mantain position how? RRS feed

  • Question

  • User855106052 posted

    Hello guys

    I have a form with dropdown, textbox, radio buttons etc and in some of these radios and dropdowns I have AutoPostBack = "true" OnSelectedIndexChanged = "VerificaPickUP". When the user chooses some options in the radiobutton or dropdownm triggers an action and returns to the top of the page but I wanted him to stay on the place. So, for this I used <% @ Page Language = "VB" Debug = "true" uiculture = "auto" MaintainScrollPositionOnPostback = "true"%> and so far all right.

    The problem is that when the user clicks the submit form button, in case of error should go to the top of the page and as I am using the MaintainScrollPositionOnPostback = "true" it stays in the same position.


    Summarizing how can I make it so that when it is autopostback of the radiobuttons and dropdowns it will hold the position and when will submit go to the top?

    Thanks for the help.

    	Sub VerificaPickUP()
    		If RadPickUPSim.Checked Then
    			PanDetalhesPickUP.Visible = True
    			ChkPickUP.Checked = True
    			If CbxPassageirosCod.SelectedValue()<>0 Then
    				IndicaPreco()
    			End If
    		Else
    			PanDetalhesPickUP.Visible = False
    			ChkPickUP.Checked = False
    			If CbxPassageirosCod.SelectedValue()<>0 Then
    				IndicaPreco()
    			End If
    		End If
    	End Sub
    
    	Sub Submit()
    		Try
                            ... 
    		Catch
    			LblInfoErro.Text = "There was an error saving the information. Please make sure you have filled in all the fields. All fields are required to guarantee the service."
    			PanMostraErro.Visible = True
    		        PanMostraSucesso.Visible = False
    		End Try
    
    	End Sub
    

    Friday, April 12, 2019 9:10 AM

All replies

  • User409696431 posted

    Reference the following article for an explanation of how to optionally override MaintainScrollPositionOnPostback = "true", without turning it off for subsequent postbacks.

    http://www.4guysfromrolla.com/articles/111407-1.aspx

    In your button handling code, when you want the postback to go back to the top of the page, change it to the following:

    LblInfoErro.Text = "There was an error saving the information. Please make sure you have filled in all the fields. All fields are required to guarantee the service."
    PanMostraErro.Visible = True
    PanMostraSucesso.Visible = False
    If Not ClientScript.IsClientScriptBlockRegistered(Me.GetType(),"CreateResetScrollPosition") Then
       ClientScript.RegisterClientScriptBlock(Me.GetType(), "CreateResetScrollPosition", _
             "function ResetScrollPosition() {" & vbCrLf & _
             " var scrollX = document.getElementById('__SCROLLPOSITIONX');" & vbCrLf & _
             " var scrollY = document.getElementById('__SCROLLPOSITIONY');" & vbCrLf & _
             " if (scrollX && scrollY) {" & vbCrLf & _
             "    scrollX.value = 0;" & vbCrLf & _
             "    scrollY.value = 0;" & vbCrLf & _
             " }" & vbCrLf & _
             "}", True)
    ClientScript.RegisterStartupScript(Me.GetType(), "CallResetScrollPosition","ResetScrollPosition();", True)
    End If

    Saturday, April 13, 2019 3:31 AM
  • User-893317190 posted

    Hi indesk,

    You could also use js setTimeout and window.scrollTo to override MaintainScrollPositionOnPostback .

    Below is my test code. In my code , if the text is no , I don't want to stay in the same position.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ScrollToConditionally.aspx.vb" Inherits="VbCases.ScrollToConditionally" MaintainScrollPositionOnPostback="true" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
           <div style="height:2000px"></div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>

    Code behind.

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If TextBox1.Text = "no" Then
                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "scroll", "setTimeout(function(){window.scrollTo(0,0)},0)", True)
            End If
        End Sub

    The result.

    In your case , you could put the code in your catch clause.

    Catch
    			LblInfoErro.Text = "There was an error saving the information. Please make sure you have filled in all the fields. All fields are required to guarantee the service."
    			PanMostraErro.Visible = True
    		        PanMostraSucesso.Visible = False
    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "scroll", "setTimeout(function(){window.scrollTo(0,0)},0)", True) End Try

    Best regards,

    Ackerly Xu

    Monday, April 15, 2019 5:17 AM