.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
Public Property DefaultFocus As String
Visual Basic (Usage)
Dim instance As HtmlForm
Dim value As String

value = instance.DefaultFocus

instance.DefaultFocus = value
C#
public string DefaultFocus { get; set; }
Visual C++
public:
property String^ DefaultFocus {
    String^ get ();
    void set (String^ value);
}
JScript
public function get DefaultFocus () : String
public function set DefaultFocus (value : String)
ASP.NET
<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 ("").
Remarks

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.

Examples

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.

Visual Basic
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="Server">

  Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    ' 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
  End Sub

 </script>

<html  >

<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>
C#
<%@ 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  >

<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>
Platforms

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.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Page view tracker