簡體   English   中英

超鏈接回發后維護滾動位置

[英]Maintaining scroll position after hyperlink postback

我已經嘗試了很多不同的方法解決我的問題,但似乎沒有一個方法可以解決問題。 我基本上使用asp.net超鏈接激活和停用用戶,問題是你一旦這樣做,頁面會因為它創建的回發而向上滾動,所以如果你有一個列表,那么滾動回來會很煩人1000個用戶。 這是代碼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; "));

根據我的理解,您可以通過以下三種方式將MaintainScrollPositionOnPostback設置為true。

  1. Web.config Level => pages maintainScrollPositionOnPostBack="true" />
  2. Page Level => <%@ Page MaintainScrollPositionOnPostback="true" %>
  3. Code Level => 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上設置滾動位置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM