一、 private void RetainScrollPosition() { StringBuilder saveScrollPosition = new StringBuilder (); StringBuilder setScrollPosition = new StringBuilder ();
RegisterHiddenField("__SCROLLPOS", "0");
saveScrollPosition.Append("<script language='javascript'>"); saveScrollPosition.Append("function saveScrollPosition() {"); saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = MyBody.scrollTop;"); saveScrollPosition.Append("}"); saveScrollPosition.Append("MyBody.onscroll=saveScrollPosition;"); saveScrollPosition.Append("</script>");
RegisterStartupScript("saveScroll", saveScrollPosition.ToString());
if (Page.IsPostBack) { setScrollPosition.Append("<script language='javascript'>"); setScrollPosition.Append("function setScrollPosition() {"); setScrollPosition.Append(" MyBody.scrollTop = " + Request["__SCROLLPOS"] + ";"); setScrollPosition.Append("}"); setScrollPosition.Append("MyBody.onload=setScrollPosition;"); setScrollPosition.Append("</script>");
RegisterStartupScript("setScroll", setScrollPosition.ToString()); } }
page_load 时,执行RetainScrollPosition() 页面html中,body加id=MyBody,即可这样一次添加,只要继承的页面都会实现你想要的了 (引用别人的,原作者我忘了,在此感谢)
二、
在你需要的位置添加一个对象控件,让页面刷新后定位到此控件的位置,使用了scrollIntoView方法。
public void ScrollToObject(string str_Ctl_Name,Page page) { if(!page.IsStartupScriptRegistered ("ScrollObject")) { page.RegisterStartupScript("msgScrollObject","
<script>document.forms(0)."+str_Ctl_Name+".scrollIntoView();document.forms(0)."+str_Ctl_Name+".focus(); </script>"); } }
注:ScrollToObject:方法名 str_Ctl_Name:控件名 例子:ScrollToObject(Button1,Page);
(来自 “阿好空间” http://blog.csdn.net/hbzxf)
|
请发表评论