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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cursor in login text box on page load!

Status
Not open for further replies.

arodri

Technical User
Jul 9, 2010
121
US
Hello,

I have a login page that requires only a password from the user. The password field is a text box and I'm trying to get the cursor to automatically be in the text box when they first load the page. We're using C# for the page, but from what I hear it might require Javascript? I'm very new to working with asp pages so I wouldn't even know where on the page I would put the code. I've tried "txtpassword.Focus();" but when I click on the link to our page the cursor is still not there. I've also read a lot about using javascript so I've tried the following code with no luck (I changed TextBox1 to the name of my textbox but that didn't work):

Code:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
StringBuilder myJavaScript = new StringBuilder();

myJavaScript.Append("<script language='Javascript'> function
setfocus(){");
myJavaScript.Append("document.getElementById(\"" +
TextBox1.ClientID + "\").focus();");
myJavaScript.Append("} setfocus();</script>");

this.RegisterStartupScript("SetFocusScript",
myJavaScript.ToString());
}


I've also seen a lot of code with "form1" and they say to just change it to what my form is, but I've looked and cannot find "form" anywhere in my code.

Please help!

Thanks in advance.


 
If you are using asp.net 2.0, try the following code:

Code:
<form id="Form1" method="post" runat="server" defaultfocus="txtUserName">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top