ASP.net Postback - Scroll to Specific Position

后端 未结 9 1025
天命终不由人
天命终不由人 2020-12-15 16:50

I have an ASP.net WebForms page that has a lot of content on the top of the screen. It has a link button that will post back to the page and show another section of the page

9条回答
  •  感情败类
    2020-12-15 17:22

    While not elegant for your situation, it is also possible to use dummy Custom Validators, set the one you want to scroll to as invalid then do

    DummyCustomValidator.SetFocusOnError = true;
    

    In my case, I am actually using page Validators with async postbacks and multiple programmatically shown/hidden panels on a long vertical form. Since some fields are only required if MyLogicalParent.Visible = true and if specific answers are given in other controls, such as a RequiredFieldValidator on a TextBox when "Other" is selected in a CheckBoxList, I have a LOT of logic to process page validation. Setting scroll positions was painful in all of the normal methods.

    I also use RegisterStartupScript to handle maintaining the current scroll position when async postbacks alter the page's vertical dimension.

        
    
         <%-- This stores the Y scroll location.--%>
            
            
    Scroll down for more.
    private string LastScrolled = ""; protected void Page_PreRender (object sender, EventArgs e) { if (string.IsNullOrEmpty(LastScrolled)) { LastScrolled = "0"; } if (string.IsNullOrEmpty(scrollPosition)) { sb.Clear(); sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);"); sb.AppendLine("function EndRequestHandler(sender, args) {"); sb.Append("scrollTo(0, ").Append(LastScrolled).Append(");"); sb.AppendLine("}"); sb.AppendLine("function load() {"); sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);"); sb.AppendLine("}"); cs.RegisterStartupScript(GetType(), "ScrollToLastPosition", sb.ToString(), true); scrollPosition = "ScrollToLastPosition"; } if (!string.IsNullOrEmpty(scrollPosition)) { ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true); } } protected void Page_Load (object sender, EventArgs e) { LastScrolled = HF_LastScrolled.Value; Page.MaintainScrollPositionOnPostBack = false; } protected void SetScrollToLastPosition () { sb.Clear(); sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);"); sb.AppendLine("function EndRequestHandler(sender, args) {"); sb.Append("scrollTo(0, ").Append(LastScrolled).AppendLine(");"); sb.AppendLine("}"); sb.AppendLine("function load() {"); sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);"); sb.AppendLine("}"); cs.RegisterStartupScript(GetType(), "ScrollToLastPosition", sb.ToString(), true); scrollPosition = "ScrollToLastPosition"; string tempstring = sb.ToString(); ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true); } protected void SetScrolltoPositionY (int y) { sb.Clear(); sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);"); sb.AppendLine("function EndRequestHandler(sender, args) {"); sb.Append("scrollTo(0, ").Append(y).AppendLine(");"); sb.AppendLine("}"); sb.AppendLine("function load() {"); sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);"); sb.AppendLine("}"); cs.RegisterStartupScript(GetType(), "ScrollTo-0-" + y.ToString(), sb.ToString(), true); scrollPosition = "ScrollTo - 0-" + y.ToString(); string tempstring = sb.ToString(); ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true); }

提交回复
热议问题