2012-01-08 42 views
1

我有以下设置时,顶部:的UpdatePanel和滚动点击一个按钮

的UpdatePanel - >多视图 - > 3次

当我在一个视图中点击一个按钮,它应该进入下一个视图并且缓慢地滚动到顶部。

目前我一直在尝试下面的方法,其中没有工作(即使不是一个快速滚动 - 它只是停留在我还在笑;-)):

protected void PartOneContinue_Click(object sender, EventArgs e) 
{ 
    CheckoutFlow.SetActiveView(PaymentMethodView); 
    ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "<script>scrollTo(0,0);</script>", true); 
} 

及以下:

ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "<script>$(window).scrollTop(100);</script>", true); 

和以下其中ScrollToTop是从上方与两个不同的脚本的函数:

ClientScriptManager cs = Page.ClientScript;  
cs.RegisterStartupScript(this.GetType(), "ScrollToTop", "ScrollToTop()"); 

任何想法将不胜感激。

+0

看一看这个帖子http://stackoverflow.com/questions/1174863/javascript-scrollto-method-does-nothing – 2012-01-08 13:59:59

+0

没有运气,很遗憾。我尝试过: ScriptManager.RegisterStartupScript(this,this.GetType(),“scroll”,“$('#LeveringsDiv')。animate({scrollTop:elementOffset},200);”,true); 其中leveringsdiv是视图底部的div,按钮很有趣。 – 2012-01-08 14:21:31

回答

0

你检查了你的文档类型吗?同时将html和body的(css)高度设置为100%可能会有所帮助。

+0

您对doctype有什么意思?我会测试100% – 2012-01-08 14:41:10

+0

我认为它需要过渡性的dtd:<!DOCTYPE html PUBLIC“ - // W3C // DTD XHTML 1.0 Transitional // EN” “http://www.w3.org/TR/xhtml1 /DTD/xhtml1-transitional.dtd“> – 2012-01-08 14:43:48

+0

我有这个,仍然有问题。 – 2012-01-08 14:53:51

0

在您的通话ScriptManager.RegisterStartupScript你逝去的脚本,包括<script>标签,也传递trueaddScriptTags参数。我的猜测是,是在你的页面呈现的脚本块看起来是这样的:

<script type="text/javascript"> 
<script>scrollTo(0,0);</script> 
</script> 

我可以想像,这不因为嵌套脚本标记的大多数浏览器。尝试从您的JavaScript片段删除标记或通过falseaddScriptTags

// Option 1 
ScriptManager.RegisterStartupScript(this, this.GetType(), 
    "myscript", "scrollTo(0,0);", true); 

// Option 2 
ScriptManager.RegisterStartupScript(this, this.GetType(), 
    "myscript", "<script>scrollTo(0,0);</script>", false); 

如果这没有解决您的问题,那么你可以考虑使用客户端脚本解决这个纯粹的,使用PageRequestManager.pageLoaded事件:

在页面上的所有内容被刷新后,引发pageLoaded事件,无论是由于同步 (整页)回发还是异步回发刷新。您可以使用此事件为更新的内容提供自定义过渡效果。

+0

我刚试过这个,没有任何反应。任何想法如何跟踪究竟是什么造成问题? – 2012-01-08 14:53:42

+0

使用Chrome开发人员工具或Firefox Firebug查看您之前是否有两个嵌套脚本标记,看看DOM发生更新后的DOM。也可以使用JavaScript控制台中的任何JavaScript错误编辑您的问题。 – 2012-01-08 15:11:43

1

试试这个方法

private void SubirTelaTopo() 
    { 
     StringBuilder csText = new StringBuilder(); 
     csText.Append("$('body,html').animate({ scrollTop: 0 }, 500);"); 
     ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), csText.ToString(), true); 
    }