我已经尝试了许多不同的方法来解决问题,但似乎都没有成功。我基本上使用 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(" "));
根据我的理解,您可以通过以下三种方式中的任何一种将 MaintainScrollPositionOnPostback 设置为 true。
- Web.config 级别 =>
pages maintainScrollPositionOnPostBack="true" />
- 页面级别 =>
<%@ Page MaintainScrollPositionOnPostback="true" %>
- 代码级别 =>
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);
});
在目标屏幕上,从查询字符串访问此滚动位置并在 dom Ready 上设置滚动位置