Problem solve Get help with specific problems with your technologies, process and projects.

Statically focus a control

How to do it and why you would need to statically focus a control.

Simple Web applications with static content seldom need any cleverness (or much code, for that matter). However, when programmers write complex applications that have pages that contain a lot of controls, keeping track of user activity and facilitating data input can be highly important. This tip, excerpted from InformIT, discusses the problem of statically focusing a control, which can obviate some of the bookkeeping requirements, and thus improve efficiency of the Web service.


Simple Web applications with static content seldom need any cleverness (or much code, for that matter). However, when programmers write complex applications that have pages that contain a lot of controls, keeping track of user activity and facilitating data input can be highly important.

For example, if one is writing applications that are especially geared toward "heads-down data entry," reducing the number of extra steps can make a big difference in productivity.

Recently, while working on a large Web application, we devised several collapsible regions that mitigated the need for multiple trips to the Web server. Unfortunately, when the rendered page returned to the client, the expanded region and focused control were lost. To solve this problem, dynamic JavaScript was generated and emitted to the client, in the form of a startup block, to refocus the point of last interest.

Let's begin by looking at the static script necessary to focus a control and then we'll convert this script to a dynamically rendered version.

Listing 1 demonstrates a static script block that focuses the same control every time. Startup script is a block of script that is placed within the <form> tag and is run as the page is loaded.

Listing 1 Static Script to Refocus the TextBoxAddress Control When a Page is Rendered.

  <script language="javascript"> 
   var control = document.getElementById("TextBoxAddress");  
   if( control != null ) control.focus(); 
  </script>
  </form>
 </body>
</html>

The script requests the control by its object name. If the control was successfully found and returned, the control.focus() method is called.


To read the entire article from which this tip comes, click over to InformIT. You don't even have to register there: Just get the info you need.


Dig Deeper on ASP.NET development best practices

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close