Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

I have three text boxes in my html page,Suppose when i load the web browser the control has to go to third text box ?

I need help to write the C# code ,,as my browser loads the page my control has to go to third text box automatically

2 Answers

Steven Parker
Steven Parker
231,236 Points

By "control" I assume you mean focus. So try this:

yourpage.html
<body onload='document.getElementById("ID_of_third_text_box").focus();'>
<!-- ...rest of document with the 3 text fields.... -->
</body>

Be sure to replace the ID_of_third_text_box with the actual ID of the third text box.

But this is HTML/JavaScript - not really a C# question.

i want to do this focus of text box from aspx.cs file

Steven Parker
Steven Parker
231,236 Points

OK, that explains the C# classification. But be aware that focus is a client-side concept, it's not something that can be accomplished in the server.

See my other comment on the cs method suggestion.

Gautham Botta
Gautham Botta
843 Points

In the Page_Load method, you can use TextBox3.Focus(); This should bring the focus to third text box when the page loads.

Steven Parker
Steven Parker
231,236 Points

:point_right: While the control.Focus() method does exist in ASP, it gets implemented internally by adding more than 90 lines of JavaScript code to the page. This mass of JavaScript code is then executed in the client after page loading. This seems like massive overkill for something that can be done with a one-liner.

And, I can tell you from personal experience, it's not always effective when applied to complex pages that host custom controls.