简体   繁体   English

超链接回发后维护滚动位置

[英]Maintaining scroll position after hyperlink postback

I have tried numerious different approaches to my problem, but none of them seem to work out. 我已经尝试了很多不同的方法解决我的问题,但似乎没有一个方法可以解决问题。 I basically activate and deactivate users using a asp.net hyperlink and the problem is as soon as you do that, the page scrolls back up because of the postback it creates, so it will be annoying to scroll back down if you have a list of 1000 users. 我基本上使用asp.net超链接激活和停用用户,问题是你一旦这样做,页面会因为它创建的回发而向上滚动,所以如果你有一个列表,那么滚动回来会很烦人1000个用户。 Here is the code iv'e been trying out without success! 这是代码iv'e一直尝试没有成功!

            // I use this variable for navigating the url for my hyperlink
            var toggleUrl = "AdminListUsers.aspx?column=" + (IsClicked.FirstOrDefault().Key ?? "Name") + "&direc=" + (IsClicked.FirstOrDefault().Value) + "&a=chstat&q=" + id.ToString() + "&d=" + disabled + "&z=" + Server.UrlEncode(txtSearchFor.Text); 

            var hl = new HyperLink(); //These hyperlinks are the same
            hl.Text = status;
            hl.Style.Add(HtmlTextWriterStyle.Color, (disabled ? "red" : "green"));
            hl.NavigateUrl = toggleUrl;
            hl.Attributes.Add("onclick", "saveScroll(this);return true;");
            cell.Controls.Add(hl);
            tr.Cells.Add(cell);

            cell = new TableCell();
            cell.Width = new Unit("10%");

            cell.Controls.Add(new LiteralControl("<nobr>"));

            var linkbtn = new HyperLink //These hyperlinks are the same
            {
               //Here as you can see are my attributes for the hyperlink
                NavigateUrl = toggleUrl,
                Width = 16,
                Height = 16,
                CssClass = disabled ? "user-status-disabled" : "user-status-enabled"
            };
            linkbtn.Attributes.Add("id", "aButton_" + id);

            ScriptManager.RegisterStartupScript(Page, typeof(Page), "ScrollToADiv", "setTimeout(scrollToDiv, 1);", true); // Not working
            linkbtn.Attributes.Add("onclick", "window.scrollTo(0, location.hash);"); // Not working either

            cell.Controls.Add(linkbtn);
            cell.Controls.Add(new LiteralControl("&nbsp; "));

As per my understanding, you could set the MaintainScrollPositionOnPostback to true in any of the below three ways. 根据我的理解,您可以通过以下三种方式将MaintainScrollPositionOnPostback设置为true。

  1. Web.config Level => pages maintainScrollPositionOnPostBack="true" /> Web.config Level => pages maintainScrollPositionOnPostBack="true" />
  2. Page Level => <%@ Page MaintainScrollPositionOnPostback="true" %> Page Level => <%@ Page MaintainScrollPositionOnPostback="true" %>
  3. Code Level => Page.MaintainScrollPositionOnPostBack = true; Code Level => Page.MaintainScrollPositionOnPostBack = true;

Hope this Helps!! 希望这可以帮助!!

EDIT Code help for the comment below (assuming jQuery is being used): 以下评论的编辑代码帮助(假设正在使用jQuery):

$(".user-status-enabled").on("click", function() {
     var $this = $(this);
     var scrollPosition = $this.scrollTop();
     $this.attr("href", $this.attr("href") + "&scrollPosition=" + scrollPosition);
});

And on the target screen, access this scrollPosition from Query String and set the scroll position on dom Ready 在目标屏幕上,从查询字符串访问此scrollPosition并在dom Ready上设置滚动位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM