Microsoft KB Archive/316719

= How to set focus to Web Form controls by using client-side script =

Article ID: 316719

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q316719





SUMMARY
This article demonstrates how to set focus to an ASP.NET Web Form control by using client-side script.

ASP.NET Web Form controls provide a similar look and feel of the traditional HTML controls while they provide a consistent and structured interface and more robust features. In addition, you can use client-side scripting to enhance the functionality that these controls provide.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET
 * Microsoft Internet Information Services (IIS) 5.0 or a later version of IIS

This article assumes that you are familiar with the following topics:
 * Web applications
 * ASP.NET
 * Visual Basic 2005 or Visual Basic .NET

Using client-side script in Visual Studio 2005 or in Visual Studio .NET
ASP.NET Web Form controls offer the appearance and functionality of the traditional HTML controls by generating the HTML elements on the client browser when the page is loaded. But unlike the HTML counterparts, ASP.NET Web Form controls provide a consistent and structured interface and more features, such as automatic postback, and the ability to generate multiple HTML elements from a single control. You can also use client-side scripting with these controls to provide additional functionality, such as setting the focus on an ASP.NET Web Form control.

The following procedure creates an ASP.NET Web application that displays three ASP.NET TextBox controls with the corresponding CommandButton controls for setting their focus. These CommandButton controls invoke client-side JavaScript to dynamically set focus on a specified server-side control:  Start Visual Studio 2005 or Visual Studio .NET. Create a new ASP.NET Web Application project in Visual Basic .NET or in Visual Basic 2005, and then name it ClientSideScriptExample . Switch to the HTML view of the WebForm1.aspx window.  In WebForm1's HTML window, copy and paste the following code between the opening and closing form tags. It displays the two TextBox controls with the corresponding command buttons:

Note When you paste the code into the HTML window, it is important to paste the code segments as HTML. To do this, click the Paste as HTML command on the context menu. InputBox 1:  InputBox 2:  Click a button to set focus on the specified control:     Copy and paste the following code into your page. Each command button invokes a client-side JavaScript function that sets focus on a particular control on the form. Be sure to position the code block before the first <BODY> tag: function cmdButton1_Clicked {  document.all('txtInput1').focus; return false; }           function cmdButton2_Clicked {  document.all('txtInput2').focus; return false; }                   </li> Click Save.</li> On the Debug menu, click Start to build and run the application. WebForm1 is displayed on the screen. Two input boxes and two corresponding command buttons are displayed.</li> Click the InputBox 1 button and the focus moves to the txtInput1 control. Click the InputBox 2 button. Focus moves to the txtInput2 control.</li> Close the browser.</li></ol>

Verification

 * 1) Click the InputBox 1 button to shift focus to the txtInput1 control.
 * 2) Click the InputBox 2 button to shift focus to the txtInput2 control. This code should not cause a call to the server and should not reload the page.

Complete code listing
<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;ClientSideScriptExample.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> WebForm1 <meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio.NET 7.0&quot;> <meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic 7.0&quot;> <meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;> <meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;> <script language=&quot;javascript&quot;> function cmdButton1_Clicked {  document.all('txtInput1').focus; return false; }           function cmdButton2_Clicked {  document.all('txtInput2').focus; return false; }   </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> InputBox 1: <asp:TextBox ID=&quot;txtInput1&quot; Runat=&quot;server&quot; Width=&quot;50&quot; /> InputBox 2: <asp:TextBox ID=&quot;txtInput2&quot; Runat=&quot;server&quot; Width=&quot;50&quot; /> Click a button to set focus on the specified control: <input ID=&quot;cmdButton1&quot; type=&quot;button&quot; value=&quot;InputBox 1&quot; OnClick=&quot;return cmdButton1_Clicked&quot;> <input ID=&quot;cmdButton2&quot; type=&quot;button&quot; value=&quot;InputBox 2&quot; OnClick=&quot;return cmdButton2_Clicked&quot;> </HTML>