Home > Microsoft .Net Development Tips > ASP.NET Development > Setting the focus on an ASP.NET page
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ASP.NET DEVELOPMENT

Setting the focus on an ASP.NET page


Roger McCook
11.18.2002
Rating: -4.17- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This tip was submitted to the Visual Studio .NET Info Center Tip Exchange by member Roger McCook. Let other users know how useful it is by rating the tip below. Got a tip or code of your own you'd like to share? Submit it here.


The aggravation
You are traveling  to a web page where you will enter data. Your fingers hover expectantly over the keyboard. A thin line of saliva trickles down your chin. You dry your face in your sleeve without moving your fingers from the keyboard. Boom! The page has loaded but ... where is the cursor? Arrrrgh! You've got to reach over and grab the mouse and click in the textbox. Now you are ready to start typing. Sound familiar? 

Big deal
Is this a big deal? To the person who uses the page occasionally, it would be nice if the cursor were automatically set to where they need to start typing. Nice and thoughtful but not that big of a deal. To the person chained to the desk, using your page all day, it is a very big deal.

The solution
Here is the solution that seems to work best for me. In the code-behind file for a page (if you use code-behind), create a procedure named SetFocus as follows:

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
     Dim s


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
ASP.NET Development
How to use jQuery to solve Javascript browser compatibility problems
How to write an out-of-browser Silverlight 3 application in 3 steps
Silverlight 3 beta SDK download lets developers try new RIA features
Visual Studio's IntelliSense for jQuery doesn't autocomplete correctly
Dundas Map for .NET kicks up geographic visualization
Return to CodePlex: Into the Sandcastle…
VBScript Tutorial
Use PHP with Visual Studio to create Web sites
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Visual Studio 2008 and .NET Framework 3.5 SP1 introduces ADO .NET Entity Designer

ASP.NET development best practices
Introduction to ASP.NET's Model View Controller (MVC) Design Pattern
Silverlight, Ajax components require different approach to UI
LINQ Learning Guide: LINQ and Web applications
VB code download home page
VB code: File manipulation downloads
Localization practices for .NET 2.0: It's still about the architecture
Creating custom ASP.NET 2.0 profile providers
.NET development in the trenches
Microsoft developers balancing age-old issues, barrage of new technology
SearchVB.com's Podcast Page

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
136 browser colors with names  (SearchWinDevelopment.com)
ASP.NET  (SearchWinDevelopment.com)
browser  (SearchWinDevelopment.com)
Document Object Model  (SearchWinDevelopment.com)
domain name  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


As String = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.ID & "').focus() </SCRIPT>"
     RegisterStartupScript("focus", s)
End
Sub

The following shows how you'd call the procedure to set the focus on a textbox named txtSerialNumber when the form first loads. This also will be in the code-behind file:

If Page.IsPostBack = False Then
    ' Blah Blah Blah All the other stuff you want to do
  
Me.SetFocus(Me.txtSerialNumber)
End If

Conclusion
Works for me, man. If you have a more elegant solution, let me know. You can email me at
RogerMcCook@hotmail.com.

Roger D. McCook
McCook Software, Inc.


Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts