Save scroll position AND setFocus

J

JC

I'm sure you've all seen the save scroll position from 4 guys from
rolla which can be found here >
http://aspnet.4guysfromrolla.com/articles/111704-1.aspx
BUT
try to get that to work AND still be able to set the focus to a
control.

Here's the problem, when you set the focus by using the
registerstartupscript method the control gets the focus after the
scroll position has been set. When this happens the scroll position
gets set a second time but it sets the focused control at the bottom
of your page.
To test-
(you would need the scroll position code from the 4 guys site)
Build a page with a whole lot of <br>'s and in the middle put a
textbox and at the end put a button. In the button call

RegisterStartupScript("focus", "<script language=""JavaScript"">" &
vbCrLf & vbTab & "document.getElementById('TextBox3').focus();" &
vbCrLf & "<" & "/script>")

I want my page to stay scrolled to the bottom but it scrolls to the
point of the textbox.

Any ideas on how to fix this would be AMAZINGLY appreciated. I don't
need to use this code just need it to work.
No, smartnavigation will not work since you can not focus with that
either
(or if you can, let me know but I'd rather not use it)

contact me at the e-mail or at ujjc001@ on my gmail account but please
post here too for others to share.
 
B

bruce barker

you need to set the focus after the scroll has happened. your focus scroll
changes the saved coordinates. there scroll happens at onload, and you need
to let the window scroll happen first. change their code to something like:

function sstchur_SmartScroller_Scroll()
{
var x = document.forms[formID].xCoordHolder.value;
var y = document.forms[formID].yCoordHolder.value;
document.forms[formID].focusHolder.value;
window.scrollTo(x, y);

window.setTimeout("document.forms[document.forms[formID].focusHolder.value].
focus()",10);
}

where you store the focus field name in focusHolder


-- bruce (sqlwork.com)


| I'm sure you've all seen the save scroll position from 4 guys from
| rolla which can be found here >
| http://aspnet.4guysfromrolla.com/articles/111704-1.aspx
| BUT
| try to get that to work AND still be able to set the focus to a
| control.
|
| Here's the problem, when you set the focus by using the
| registerstartupscript method the control gets the focus after the
| scroll position has been set. When this happens the scroll position
| gets set a second time but it sets the focused control at the bottom
| of your page.
| To test-
| (you would need the scroll position code from the 4 guys site)
| Build a page with a whole lot of <br>'s and in the middle put a
| textbox and at the end put a button. In the button call
|
| RegisterStartupScript("focus", "<script language=""JavaScript"">" &
| vbCrLf & vbTab & "document.getElementById('TextBox3').focus();" &
| vbCrLf & "<" & "/script>")
|
| I want my page to stay scrolled to the bottom but it scrolls to the
| point of the textbox.
|
| Any ideas on how to fix this would be AMAZINGLY appreciated. I don't
| need to use this code just need it to work.
| No, smartnavigation will not work since you can not focus with that
| either
| (or if you can, let me know but I'd rather not use it)
|
| contact me at the e-mail or at ujjc001@ on my gmail account but please
| post here too for others to share.
 
J

JC

Bruce, thank you very much. I was able to use your idea of setTimeout
and add it to my code.

Public Shared Sub SetFocus(ByVal strControl As String, ByVal frm As
Page)
frm.RegisterStartupScript("focus", "<script
language=""JavaScript"">" & vbCrLf & vbTab &
"window.setTimeout(""document.getElementById('" & strControl &
"').focus()"",10);" & vbCrLf & "<" & "/script>")
End Sub

That's the function as it sits, quite simple! Again, thanks for the
perfect answer.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top