Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Web.UI
Page Class
Page Methods
SetFocus Method
 SetFocus Method (Control)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
Page.SetFocus Method (Control)

Note: This method is new in the .NET Framework version 2.0.

Sets the browser focus to the specified control.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

Visual Basic (Declaration)
Public Sub SetFocus ( _
    control As Control _
)
Visual Basic (Usage)
Dim instance As Page
Dim control As Control

instance.SetFocus(control)
C#
public void SetFocus (
    Control control
)
C++
public:
void SetFocus (
    Control^ control
)
J#
public void SetFocus (
    Control control
)
JScript
public function SetFocus (
    control : Control
)

Parameters

control

The control to receive focus.

Exception typeCondition

ArgumentNullException

control is a null reference (Nothing in Visual Basic).

InvalidOperationException

SetFocus is called when the control is not part of a Web Forms page.

- or -

SetFocus is called after the PreRender event.

Use the SetFocus method to make the specified control the active control on the Web page displayed by the browser. The SetFocus method must be called before the page is prepared for rendering to the client in the PreRender event.

NoteNote

The SetFocus method will work only on browsers supporting ECMAScript version 1.3 or later.

TopicLocation
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications in Visual Studio
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications in Visual Studio

The following code example sets the focus to the second text box on a Web page.

Visual Basic
<%@ page language="VB" %>

<script runat="server">
    Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        SetFocus(focusHere)
    End Sub
</script>

<html>
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="default" runat="server" /><br />
        <asp:textbox id="focusHere" runat="server" />
    </form>
</body>
</html>
C#
<%@ page language="C#" %>

<script runat="server">
    void Page_Init(object sender, EventArgs e)
    {
        SetFocus(focusHere);
    }
</script>

<html>
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="default" runat="server" /><br />
        <asp:textbox id="focusHere" runat="server" />
    </form>
</body>
</html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker