Adding JavaScript to focus on a section of the page after btn oncl

G

Guest

I have a button at the top of my page that I have an onClick event handler
for which makes some new controls (at the bottom of my page) visible. When
the user clicks the button I make the new controls visible in the onclick
event handler, but I do not know how to bring the focus to the bottom section
of the page. So basically, when the user clicks the button, everything works
correctly in the onclick (all controls are made visible), however when the
page is displayed, the user would have to scroll down to the bottom to find
the new controls.

I'm looking for a way to bring the attention to the bottom section of the
page where the new controls are. Remember that I have to use the onclick
event handler to first make the controls visible. Any good ideas?
 
D

Damon Payne

If there is a focusable field (like a text box) at the bottom of the page,
you could do a RegisterStartupScript to put focus on that control, the
browser should then scroll it to visible.
 
G

Guest

There is a dropdown list (asp:DropdownList) at the bottom of the page, but it
is not even visible until the user clicks the button at the top of the page.

When the user clicks the button, the onclick event handler (server side)
makes a dropdownlist control at the bottom of the page visible, along with
some other disabled fields. I want to be able to focus on that dropdownlist,
or just that section of the page in general so the user doesn't have to
scroll.

Not sure how to accomplish this.
 
D

Damon Payne

Yes, that makes sense.
To put focus in that field afte the button click, you could put code
something like this in your event handler. Assume your dropdown list is
entitled "ddl"
....
string script = "<script>document.getElementById('{0}').focus() </script>
Page.RegisterStartupScript("focusDdl", string.Format(script, new
object[]{ddl.ClientId});
....

The page will run that javascript, and focus your field. If a field is
focused the browser will scroll to where the field is visible.
 
G

Guest

Thanks Damon! That works perfectly. I also added a check in my event
handler to see if the script was already registered. I assume I should do
this, if not please advise.

if (!IsStartupScriptRegistered("focuslstShortTitles"))
{
string script = "<script> document.getElementById('{0}').focus() </script>";
Page.RegisterStartupScript("focuslstShortTitles", string.Format(script,
new object[]{lstShortTitles.ClientID}));
}

Paul

Damon Payne said:
Yes, that makes sense.
To put focus in that field afte the button click, you could put code
something like this in your event handler. Assume your dropdown list is
entitled "ddl"
....
string script = "<script>document.getElementById('{0}').focus() </script>
Page.RegisterStartupScript("focusDdl", string.Format(script, new
object[]{ddl.ClientId});
....

The page will run that javascript, and focus your field. If a field is
focused the browser will scroll to where the field is visible.

--
------------------------------------------
Damon Payne
http://www.damonpayne.com
PK9 said:
There is a dropdown list (asp:DropdownList) at the bottom of the page, but it
is not even visible until the user clicks the button at the top of the page.

When the user clicks the button, the onclick event handler (server side)
makes a dropdownlist control at the bottom of the page visible, along with
some other disabled fields. I want to be able to focus on that dropdownlist,
or just that section of the page in general so the user doesn't have to
scroll.

Not sure how to accomplish this.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top