【问题标题】:Maintaining scroll position after hyperlink postback超链接回发后保持滚动位置
【发布时间】:2017-11-13 14:48:32
【问题描述】:

我尝试了许多不同的方法来解决我的问题,但似乎都没有成功。我基本上使用 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; "));

【问题讨论】:

  • P.S.您如何激活停用用户、您使用了哪些控件以及如何执行回发。 2017 只是一个建议,所以鼓励自己并开始使用 AJAX Get/Post 从/向服务器获取/发送数据。这样可以避免不必要的回发和简单操作的延迟。
  • 这是旧代码,而且代码很多,所以我没有时间重组整个项目。

标签: javascript c# jquery asp.net hyperlink


【解决方案1】:

据我了解,您可以通过以下三种方式中的任何一种将 MaintainScrollPositionOnPostback 设置为 true。

  1. Web.config 级别 => pages maintainScrollPositionOnPostBack="true" /&gt;
  2. 页面级别 => &lt;%@ Page MaintainScrollPositionOnPostback="true" %&gt;
  3. 代码级别 => Page.MaintainScrollPositionOnPostBack = true;

希望对你有帮助!!

编辑 下面注释的代码帮助(假设正在使用 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

【讨论】:

  • 是的,我也尝试过这种方法,它也不起作用。
  • 看起来您在单击链接按钮时正在导航到不同的页面,并且您正在当前页面中注册滚动到 div 脚本。
  • 我认为在新屏幕中保留滚动位置不太好,因为我们不确定是否会在屏幕上保留相同的数据。可能会添加一些新数据或删除现有数据。我建议重新访问该功能。如果没有,您可以做的一件事是在单击链接按钮时捕获顶部位置(使用 javascript),并通过添加新的查询字符串 var 将其传递到目标屏幕。
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 2013-07-17
相关资源
最近更新 更多