Export (0) Print
Expand All

HtmlForm.DefaultFocus Property

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

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)

'Declaration
Public Property DefaultFocus As String
'Usage
Dim instance As HtmlForm
Dim value As String

value = instance.DefaultFocus

instance.DefaultFocus = value
/** @property */
public String get_DefaultFocus ()

/** @property */
public void set_DefaultFocus (String value)

public function get DefaultFocus () : String

public function set DefaultFocus (value : String)

Property Value

The ID 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="VB" %>

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

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

Community Additions

ADD
Show:
© 2014 Microsoft