我尝试过以下代码:
txtField.Focus()
在我的代码后台,我设置了焦点并尝试滚动到底部,但它会立即回滚到顶部。文本框仍然是焦点所在,但屏幕位置在顶部。链接位于屏幕顶部,这导致回传。我想要滚动到屏幕底部,虽然它能够短暂地做到,但马上又会滚回到顶部。我已经尝试过设置:
Page.MaintainScrollPositionOnPostback = false;
但这似乎也没有帮助。
有没有办法强制让它跳转到特定位置?在使用按钮或链接按钮进行回发时,是否可能向URL添加锚标签?
txtField.Focus()
在我的代码后台,我设置了焦点并尝试滚动到底部,但它会立即回滚到顶部。文本框仍然是焦点所在,但屏幕位置在顶部。链接位于屏幕顶部,这导致回传。我想要滚动到屏幕底部,虽然它能够短暂地做到,但马上又会滚回到顶部。Page.MaintainScrollPositionOnPostback = false;
但这似乎也没有帮助。
有没有办法强制让它跳转到特定位置?在使用按钮或链接按钮进行回发时,是否可能向URL添加锚标签?
Page.MaintainScrollPositionOnPostBack = true;
这个设置可以让页面在postback后返回到同一位置,但您也可以使用AJAX或者SetFocus()
在postback后聚焦到指定的控件:
如果您已经有了一个锚点位置,可以使用以下代码:
Page.ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#MOVEHERE';", true);
function scrollToDiv()
{
document.getElementById('yourDiv').scrollIntoView();
}
并且在页面启动时以1毫秒的小延迟调用它(纯JavaScript)
setTimeout(scrollToDiv, 1);
ScriptManager.RegisterStartupScript(Page, typeof(Page), "ScrollToADiv", "setTimeout(scrollToDiv, 1);", true);
这样做可以绕过任何ASP自动滚动。
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) {
string targetId = Page.Request.Params.Get("__EVENTTARGET");
Page.ClientScript.RegisterStartupScript(this.GetType(), "focusthis", "document.getElementById('" + targetId + "').focus()", true);
}
}
我尝试了Matthieu Charbonnier的答案,但除非我添加了
" window.scrollTo = function () { };"
正如http://gnidesign.blogspot.com.au/2011/06/how-to-maintain-page-scroll-on-postback.html所建议的
我创建了一个辅助方法,它在Chrome、FireFox和IE上均可工作。
public static void ScrollToControl( Page page, string clientId, bool alignToTop)
{
//NOTE: if there are more than one call on the page, first one will take preference
//If we want that last will take preference, change key from MethodBase.GetCurrentMethod().Name to anchorName
//recommended in http://gnidesign.blogspot.com.au/2011/06/how-to-maintain-page-scroll-on-postback.html
String script = " window.scrollTo = function () { };" + Environment.NewLine;
script += String.Format("document.getElementById('{0}').scrollIntoView({1});" , clientId, alignToTop.JSToString());
page.ClientScript.RegisterStartupScript(TypeForClientScript(), MethodBase.GetCurrentMethod().Name, script, true );
//return script;
}
public static string JSToString(this bool bValue)
{
return bValue.ToString().ToLower();
}
使用getElementById('{0}').scrollIntoView比location.hash更简单,因为您不需要添加额外的锚点元素。
参数alignToTop非常方便,可以指定您想要在屏幕顶部还是底部显示控件。
我有
<asp:MultiView ID="mvAriza" runat="server">
<asp:View ID="View14" runat="server">
............ .......
</asp:View>
</asp:MultiView>
Page.SetFocus(mvAriza.ClientID);
它运行得很好。
Page.MaintainScrollPositionOnPostback = true
看起来运行良好。
这是一个与ASP.NET控件相关的功能,可以自动滚动到所需的Div位置。
您可以在任何地方调用此函数,并下载JavaScript文件。
在客户端单击时,使用OnClientClick="return scrollGrid()"。
function scrollGrid1() { $('html,body').animate ( { scrollTop: $('#Div1').offset().top }, 'slow' ) }
虽然不太适合您的情况,但也可以使用虚拟自定义验证器,将要滚动到的验证器设置为无效,然后执行
DummyCustomValidator.SetFocusOnError = true;
<script type="text/javascript">
$(document).ready(function () {
var windowHeight = $(document)[0].documentElement.clientHeight; /*This is the height of the viewable browser area.*/
var scrolledPosition = $(window)[0].scrollY; /*This is the number of Pixels the Window is currently scrolled to.*/
var scroll = windowHeight + scrolledPosition; /*This should be the same as $(window).scrollTop() */
/*If the amount scrolled + the height of the window is Less than or equal to the total height of the page (past the viewable client window)*/
if ($(window).scrollTop() + getWindowSize()[1] <= getDocHeight()) {
/*Move the morescroll div to the bottom of the page... -34 is the height of the div plus a small bottom margin.*/
$("#morescroll").offset({ top: windowHeight - 34 });
}
})
/*This is the total height of the document including the area past the viewable client window.*/
function getDocHeight() {
var D = document;
/*The Largest of these six numbers is the total doc height. */
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.offsetHeight, D.documentElement.offsetHeight,
D.body.clientHeight, D.documentElement.clientHeight
);
}
/*This is the width and height of the Viewable Browser area.*/
function getWindowSize() {
var myWidth = 0, myHeight = 0;
if (typeof (window.innerWidth) == 'number') {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return [myWidth, myHeight];
}
//This sets a transparent div <div id="morescroll" class="scrollMinder"> with the text "Scroll down for more." to the bottom of the viewable page.
$(window).scroll(
function () {
var windowHeight = $(document)[0].documentElement.clientHeight;
var scrolledPosition = $(window)[0].scrollY;
var scrll = windowHeight + scrolledPosition;
document.getElementById('<%= HF_LastScrolled.ClientID %>').value = scrolledPosition;
var docHeight = $(document)[0].documentElement.scrollHeight;
/*if we are scrolled to within 60 pixels from the bottom of the document, hide the indicator so it doesn't cover the footer.*/
if ($(window).scrollTop() + $(window).height() >= $(document).height() - 60) {
$("#morescroll").hide();
}
/*if we scroll back above 60 pixels from the bottom of the document, show the indicator and set the top of the div to -34 pixels.*/
else if ($(window).scrollTop() + getWindowSize()[1] <= getDocHeight()) {
$("#morescroll").show();
$("#morescroll").offset({ top: scrll - 34 });
}
});
</script>
<%-- This stores the Y scroll location.--%>
<asp:HiddenField ID="HF_LastScrolled" runat="server" />
<div id="morescroll" class="scrollMinder">
<span class="spanMinder">Scroll down for more.</span>
</div>
private string LastScrolled = "";
protected void Page_PreRender (object sender, EventArgs e)
{
if (string.IsNullOrEmpty(LastScrolled))
{
LastScrolled = "0";
}
if (string.IsNullOrEmpty(scrollPosition))
{
sb.Clear();
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("function EndRequestHandler(sender, args) {");
sb.Append("scrollTo(0, ").Append(LastScrolled).Append(");");
sb.AppendLine("}");
sb.AppendLine("function load() {");
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("}");
cs.RegisterStartupScript(GetType(), "ScrollToLastPosition", sb.ToString(), true);
scrollPosition = "ScrollToLastPosition";
}
if (!string.IsNullOrEmpty(scrollPosition))
{
ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true);
}
}
protected void Page_Load (object sender, EventArgs e)
{
LastScrolled = HF_LastScrolled.Value;
Page.MaintainScrollPositionOnPostBack = false;
}
protected void SetScrollToLastPosition ()
{
sb.Clear();
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("function EndRequestHandler(sender, args) {");
sb.Append("scrollTo(0, ").Append(LastScrolled).AppendLine(");");
sb.AppendLine("}");
sb.AppendLine("function load() {");
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("}");
cs.RegisterStartupScript(GetType(), "ScrollToLastPosition", sb.ToString(), true);
scrollPosition = "ScrollToLastPosition";
string tempstring = sb.ToString();
ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true);
}
protected void SetScrolltoPositionY (int y)
{
sb.Clear();
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("function EndRequestHandler(sender, args) {");
sb.Append("scrollTo(0, ").Append(y).AppendLine(");");
sb.AppendLine("}");
sb.AppendLine("function load() {");
sb.AppendLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);");
sb.AppendLine("}");
cs.RegisterStartupScript(GetType(), "ScrollTo-0-" + y.ToString(), sb.ToString(), true);
scrollPosition = "ScrollTo - 0-" + y.ToString();
string tempstring = sb.ToString();
ScriptManager.RegisterStartupScript(this, GetType(), scrollPosition, sb.ToString(), true);
}