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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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();
  }

</script>

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

<head>

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

</head>

<body>

  <form id="Form1"
        defaultbutton="SubmitButton"
        defaultfocus="TextBox1"
        runat="server">

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

    TextBox1:
    <asp:textbox id="TextBox1"
                 autopostback="true" 
                 runat="server">
    </asp:textbox>

    <br />

    TextBox2:
    <asp:textbox id="TextBox2"
                 autopostback="true" 
                 runat="server">
    </asp:textbox>

    <br /><br />

    <asp:button id="SubmitButton"
                text="Submit" 
                runat="server">
    </asp:button>

    <asp:button id="CancelButton" 
                text="Cancel"
                runat="server">
    </asp:button>

    <hr />

    <asp:label id="Label1"
               runat="Server">
    </asp:label>

    <asp:label id="Label2"
               runat="Server">
    </asp:label>

  </form>

</body>

</html>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft