Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Giving a TextBox control on a page load

Status
Not open for further replies.

stevenk140

Programmer
Nov 20, 2003
34
CA
I have web page and a textbox on that web page. I was wondering if there is a way for textbox to start off as 'Selected' so the user can just start typing as soon as the page loads, rather than having to click in it before he starts to type.

Steven
 
You can do it via javascript:
Code:
<head>
 function setFocus(){
  // id of your server control
  objText = document.getElementById(&quot;myTextBox&quot;);
  objText.select();
 }
</head>

<body onload=&quot;javascript:setFocus()&quot;>
 
I think you can.
Code:
string  script = &quot;<script language=javascript>&quot;;
script += &quot;function window.onload(){&quot;;
script += &quot;document.getElementById('myTextBox').select();}&quot;;
script += &quot;</script>&quot;;
if(!Page.Page.IsClientScriptBlockRegistered(&quot;setFocus&quot;))
{
 Page.RegisterClientScriptBlock(&quot;setFocus&quot;, script);
}
 
In the HTML portion for the aspx page. Place the following onload in the body tag. Obviously use the Form Name and the Text Box Name.

<body onload=&quot;document.Form1.txtUserID.focus()&quot;>


Hope everyone is having a great day!

Thanks - Jennifer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top