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

时间:2017-06-12 07:53:15

标签: javascript c# jquery asp.net hyperlink

我已经尝试了很多不同的方法来解决我的问题,但似乎没有一种方法可以解决问题。我基本上使用asp.net超链接激活和停用用户,问题是你一旦这样做,页面会因为它创建的回发而向上滚动,所以如果你有一个列表,那么滚动回来会很烦人1000个用户。以下是我们一直试用的代码没有成功!

            // 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; "));

1 个答案:

答案 0 :(得分:0)

根据我的理解,您可以通过以下三种方式将MaintainScrollPositionOnPostback设置为true。

  1. Web.config Level =&gt; pages maintainScrollPositionOnPostBack="true" />
  2. Page Level =&gt; <%@ Page MaintainScrollPositionOnPostback="true" %>
  3. 代码级别=&gt; Page.MaintainScrollPositionOnPostBack = true;
  4. 希望这有助于!!

    EDIT 下面评论的代码帮助(假设正在使用jQuery):

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

    在目标屏幕上,从查询字符串访问此scrollPosition并在dom Ready上设置滚动位置

相关问题