setting focus

N

Nate Hekman

I'm making a simple page with an edit box and a Submit button. When a user
arrives at the page, I'd like the focus to be in the edit box. I can do it
with javascript, but it seems to me there must be a way to do it with
asp.net. What's the "proper" way to do this?

Thanks.


Nate Hekman
Calgary, Alberta, Canada
 
C

Curt_C [MVP]

It's a clientside activity so it will be done with clientside code as you
indicated.
 
D

David Wier

You can add a Javascript attribute to the ASP.Net code.
from Tips and Tricks at ASPNet101.com
(http://aspnet101.com/aspnet101/tips.aspx?id=102):
Two things:
1. Give your BODY Tag and ID and include 'Runat=Server'
(like - - <body id="bdyMain" Runat="Server">)
2. In the Page_Load event:
if not Page.IsPostBack then
bdyMain.attributes("onload")="document.form1.TextBox1.focus();"
end if
David WierMCP, MVP ASP.NET,
ASPInsiderhttp://aspnet101.comhttp://aspexpress.com
 
A

Alan Ferrandiz [MCT]

Well ASP.NET runs entirely on the server. it knows nothing about focus and position in the browser that's why indeed you have to use client side code... but you do not necessarily have to place that client side code from the client you can generate the code from the server side.

RegisterStartupScript("onload", "<script type=""text/javascript"">document.forms[0]." + txtLogin.ClientID + ".focus();</script>")

Hope this helps
Alan Ferrandiz Langley [MCT]
 

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