一、
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:控件名
新闻热点
疑难解答
图片精选