Asp.net保持页面滚动条位置(页面提交后不变)
程序员文章站
2024-03-04 08:49:29
一般来说很多的人在控制浏览器滚动条都是用page.smartnavigation = false,对于页面中div等等的滚动条控制,我在看了别人代码后,写了一个,仅供参考:...
一般来说很多的人在控制浏览器滚动条都是用page.smartnavigation = false,对于页面中div等等的滚动条控制,我在看了别人代码后,写了一个,仅供参考:
public shared sub savedivscrollposition(byval dividarray as string, byval objpage as page)
dim savescrollposition as string
dim i as integer
dim divid() as string
divid = dividarray.split(",")
for i = 0 to divid.length - 1
objpage.registerhiddenfield(divid(i) & "__scrollpos", objpage.request.form(divid(i) & "__scrollpos"))
savescrollposition = "<script language='javascript'>" _
& "function savescrollposition() {" _
& " if (document.all['" & divid(i) & "'] != undefined) {" _
& "document.forms[0]." & divid(i) & "__scrollpos.value = " _
& "document.all['" & divid(i) & "'].scrolltop + ',' " _
& " + document.all['" & divid(i) & "'].scrollleft;}}" _
& "if (document.all['" & divid(i) & "'] != undefined) {document.all['" & divid(i) & "'].onscroll=savescrollposition;}" _
& "</script>"
objpage.registerstartupscript("savescroll" & divid(i), savescrollposition)
next
end sub
public shared sub restoredivscrollposition(byval dividarray as string, byval objpage as page)
dim restorescrollposition as string
dim setpositionfunction as string
dim i as integer
dim divid() as string
divid = dividarray.split(",")
for i = 0 to divid.length - 1
setpositionfunction = setpositionfunction & "setscrollposition('" & divid(i) & "');"
next
restorescrollposition = "<script language='javascript'>" _
& setpositionfunction _
& "</script>"
objpage.registerstartupscript("restorescroll", restorescrollposition)
end sub
javascript:
function setscrollposition(divid){
var e;
var a;
var obj;
if (document.getelementbyid(divid)){
obj = eval('document.forms[0].' + divid + '__scrollpos');
if (obj) {
e=eval('document.forms[0].' + divid + '__scrollpos').value;
a=e.split(',');
document.getelementbyid(divid).scrolltop= a[0];
document.getelementbyid(divid).scrollleft= a[1];
}
}
}
在页面后台调用的时候,写下如下的代码:
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
call savedivscrollposition("div1,div2,div3",me)
call restoredivscrollposition("div1,div2,div3",me)
end sub
不好意思,这两个sub可以写成一个,仅供参考。
复制代码 代码如下:
public shared sub savedivscrollposition(byval dividarray as string, byval objpage as page)
dim savescrollposition as string
dim i as integer
dim divid() as string
divid = dividarray.split(",")
for i = 0 to divid.length - 1
objpage.registerhiddenfield(divid(i) & "__scrollpos", objpage.request.form(divid(i) & "__scrollpos"))
savescrollposition = "<script language='javascript'>" _
& "function savescrollposition() {" _
& " if (document.all['" & divid(i) & "'] != undefined) {" _
& "document.forms[0]." & divid(i) & "__scrollpos.value = " _
& "document.all['" & divid(i) & "'].scrolltop + ',' " _
& " + document.all['" & divid(i) & "'].scrollleft;}}" _
& "if (document.all['" & divid(i) & "'] != undefined) {document.all['" & divid(i) & "'].onscroll=savescrollposition;}" _
& "</script>"
objpage.registerstartupscript("savescroll" & divid(i), savescrollposition)
next
end sub
复制代码 代码如下:
public shared sub restoredivscrollposition(byval dividarray as string, byval objpage as page)
dim restorescrollposition as string
dim setpositionfunction as string
dim i as integer
dim divid() as string
divid = dividarray.split(",")
for i = 0 to divid.length - 1
setpositionfunction = setpositionfunction & "setscrollposition('" & divid(i) & "');"
next
restorescrollposition = "<script language='javascript'>" _
& setpositionfunction _
& "</script>"
objpage.registerstartupscript("restorescroll", restorescrollposition)
end sub
javascript:
复制代码 代码如下:
function setscrollposition(divid){
var e;
var a;
var obj;
if (document.getelementbyid(divid)){
obj = eval('document.forms[0].' + divid + '__scrollpos');
if (obj) {
e=eval('document.forms[0].' + divid + '__scrollpos').value;
a=e.split(',');
document.getelementbyid(divid).scrolltop= a[0];
document.getelementbyid(divid).scrollleft= a[1];
}
}
}
在页面后台调用的时候,写下如下的代码:
复制代码 代码如下:
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
call savedivscrollposition("div1,div2,div3",me)
call restoredivscrollposition("div1,div2,div3",me)
end sub
不好意思,这两个sub可以写成一个,仅供参考。