如何从代码后面设置滚动位置? [英] how to set scroll postion from code behind?

查看:61
本文介绍了如何从代码后面设置滚动位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个网页。我的网页上有滚动条。

i希望在PageLoad上设置初始滚动条位置。

如何从代码后面执行此操作?

解决方案

可以使用JavaScript作为

<预郎= 使用Javascript> <跨度类= 代码sdkkeyword>文档 .getElementById(<跨度类=代码-string>' MyID')。scrollIntoView( true );



或jQuery ScrollTo

http://plugins.jquery。 com / project / ScrollTo [ ^ ]



查看此如何将ASP.NET控件滚动到视图中使用代码隐藏加载页面 [ ^ ]


开你想要滚动发生的回发事件,添加以下命令:



Page.MaintainScrollPositionOnPostBack = false;



然后,将焦点设置在您想要滚动位置的控件上。例如,如果您希望滚动位置在控件txtName上,那么您需要添加:



this.txtName.Focus();



因此,如果您不想保持滚动位置,请添加以下两个命令:



Page.MaintainScrollPositionOnPostBack = FALSE;

this.txtName.Focus();



这将取消您保持对回传和滚动滚动位置你控制的页面



这假设你设置Page.MaintainScrollPositionOnPostBack = true;在PageLoad上。否则,只需将Focus设置为页面出现时需要滚动的控件。


Hi,
i have a webpage.In my webpage has scrollbar.
i wanted to set initial scrollbar postion at perticular control on PageLoad.
How can i do this from code behind?

解决方案

You can use the javascript as

document.getElementById('MyID').scrollIntoView(true);


or jQuery ScrollTo
http://plugins.jquery.com/project/ScrollTo[^]

Check this How to scroll an ASP.NET control into view after page load using codebehind[^]


On a postback event that you want the scroll to take place, add this command:

Page.MaintainScrollPositionOnPostBack = false;

And then, set the focus on the control where you want the scroll position to be. For example, if you want the scroll position to be on control "txtName", then you'd add:

this.txtName.Focus();

So alltogether on the event that you don't want to maintain scroll position, you add these two commands:

Page.MaintainScrollPositionOnPostBack = false;
this.txtName.Focus();

That will cancel your maintain scroll position on postback and scroll the page to your control

This is assuming that you set Page.MaintainScrollPositionOnPostBack = true; on PageLoad. Otherwise, simply set Focus to the control that the page needs to scroll to when it comes up.


这篇关于如何从代码后面设置滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆