Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HtmlForm.DefaultFocus Property

Gets or sets the control on the form to display as the control with input focus when the HtmlForm control is loaded.

Namespace:  System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

public string DefaultFocus { get; set; }
<asp:HtmlForm DefaultFocus="String" />

Property Value

Type: System.String
The ClientID of the control on the form to display as the control with input focus when the HtmlForm is loaded. The default value is an empty string ("").

Use the DefaultFocus property to access the control on the form to display as the control with input focus when the HtmlForm control is loaded. Controls that can be selected are displayed with a visual cue indicating that they have the focus. For example, a TextBox control with focus is displayed with the insertion point positioned inside of it.

The control with focus can also be set using the Focus or SetFocus methods. These methods have precedence over the DefaultFocus property. If either of these methods are called to set the control with focus, the value of the DefaultFocus property is ignored.

The following code example demonstrates how to set the DefaultFocus property to display a specified control on a form as the control with input focus when the form loads. When Form1 renders, notice that the insertion point is positioned in TextBox1, indicating that TextBox1 is the control with focus.

<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">     

  void Page_Load(object sender, System.EventArgs e)

    // Set the text of the two label controls.
    Label1.Text = "The DefaultButton property is set to "
                + Form1.DefaultButton.ToString() + "<br/>";
    Label2.Text = "The DefaultFocus property is set to "
                + Form1.DefaultFocus.ToString();


<html xmlns="http://www.w3.org/1999/xhtml" >


    <title>HtmlForm DefaultButton and DefaultFocus Properties Example</title>



  <form id="Form1"

    <h3>HtmlForm DefaultButton and DefaultFocus Properties Example</h3>        

    <asp:textbox id="TextBox1"

    <br />

    <asp:textbox id="TextBox2"

    <br /><br />

    <asp:button id="SubmitButton"

    <asp:button id="CancelButton" 

    <hr />

    <asp:label id="Label1"

    <asp:label id="Label2"




Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

© 2015 Microsoft