WebControl.TabIndex 属性

定义

获取或设置 Web 服务器控件的选项卡索引。Gets or sets the tab index of the Web server control.

public:
 virtual property short TabIndex { short get(); void set(short value); };
public virtual short TabIndex { get; set; }
member this.TabIndex : int16 with get, set
Public Overridable Property TabIndex As Short

属性值

Web 服务器控件的选项卡索引。The tab index of the Web server control. 默认值为 0,指示未设置此属性。The default is 0, which indicates that this property is not set.

异常

指定的选项卡索引不在 -32768 和 32767 之间。The specified tab index is not between -32768 and 32767.

示例

下面的示例演示如何使用 WebControlTabIndex 属性来设置页上控件的 tab 键顺序。The following example illustrates how to use the TabIndex property of the WebControl to set the tab order of controls on the page.

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,则可能无法正常工作。The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。This code sample must be copied into an empty text file that has an .aspx extension. 有关 Web 窗体代码模型的详细信息,请参阅ASP.NET Web 窗体页代码模型For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

<%@ 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 SubmitBtn1_Click(Object sender, EventArgs e)
    {
        SubmitBtn1.TabIndex = 0;
        TextBox1.TabIndex = (short)((TextBox1.Text=="") ? 0 : 
            System.Int32.Parse(TextBox1.Text));
        TextBox2.TabIndex = (short)((TextBox2.Text=="") ? 0 : 
            System.Int32.Parse(TextBox2.Text));
        TextBox3.TabIndex = (short)((TextBox3.Text=="") ? 0 : 
            System.Int32.Parse(TextBox3.Text));
    }

</script>
 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Enabled Property Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>TabIndex Property of a Web Control<br /></h3>

    <p>
        Enter a number (1, 2, or 3) in each text box, <br /> 
        click the Submit button to set the TabIndexes, then <br /> 
        click on the page and tab through the page to verify.
    </p>
 
    <p><asp:Button id="SubmitBtn1" OnClick="SubmitBtn1_Click" 
            Text="Submit" runat="server"/>
    </p>

    <p><asp:TextBox id="TextBox1" BackColor="Pink" 
            runat="server"/>
    </p>
    <p><asp:TextBox id="TextBox2" BackColor="LightBlue" 
            runat="server"/>
    </p>
    <p><asp:TextBox id="TextBox3" BackColor="LightGreen" 
            runat="server"/>
    </p>  
     
    </div>
    </form>
</body>
</html>
<%@ 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 SubmitBtn1_Click(sender As Object, e As EventArgs)
        SubmitBtn1.TabIndex = 0
        If TextBox1.Text = "" Then
            TextBox1.TabIndex = 0
        Else
            TextBox1.TabIndex = System.Int16.Parse(TextBox1.Text)
        End If
        If TextBox2.Text = "" Then
            TextBox2.TabIndex = 0
        Else
            TextBox2.TabIndex = System.Int16.Parse(TextBox2.Text)
        End If
        If TextBox3.Text = "" Then
            TextBox3.TabIndex = 0
        Else
            TextBox3.TabIndex = System.Int16.Parse(TextBox3.Text)
        End If
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Enabled Property Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>TabIndex Property of a Web Control<br /></h3>

    <p>
        Enter a number (1, 2, or 3) in each text box, <br /> 
        click the Submit button to set the TabIndexes, then <br /> 
        click on the page and tab through the page to verify.
    </p>
 
    <asp:Button id="SubmitBtn1" OnClick="SubmitBtn1_Click" 
        Text="Submit" runat="server"/>
    <p>
        <asp:TextBox id="TextBox1" BackColor="Pink" 
            runat="server"/>
    </p>
    <p>
        <asp:TextBox id="TextBox2" BackColor="LightBlue" 
            runat="server"/>
    </p>
    <p>
        <asp:TextBox id="TextBox3" BackColor="LightGreen" 
            runat="server"/>
    </p>  

    </div>
    </form>
</body>
</html>

注解

使用 TabIndex 属性可以指定或确定 web 窗体页上的 Web 服务器控件的选项卡索引。Use the TabIndex property to specify or determine the tab index of a Web server control on the Web Forms page. Tab 键时,Web 服务器控件接收焦点的顺序取决于每个控件的 TabIndex 属性。When you press the Tab key, the order in which the Web server controls receive focus is determined by the TabIndex property of each control. 最初加载页面时,按下 Tab 键时获得焦点的第一项是地址栏。When a page is initially loaded, the first item that receives focus when the Tab key is pressed is the address bar. 接下来,Web 窗体页上的控件将根据每个控件的 TabIndex 属性的值以升序排列,从最小的非零值开始。Next, the controls on the Web Forms page are tabbed to in ascending order, based on the value of the TabIndex property of each control, starting with the smallest positive, nonzero value. 如果多个控件共享同一个选项卡索引,则这些控件将按其在 Web 窗体页上的声明顺序接收焦点。If multiple controls share the same tab index, the controls will receive focus in the order they are declared on the Web Forms page. 最后,选项卡索引为零的控件按其声明顺序排列。Finally, controls that have a tab index of zero are tabbed to in the order they are declared.

备注

只有具有非零选项卡索引的控件才会呈现 tabindex 特性。Only controls with a nonzero tab index will render the tabindex attribute.

可以通过将 TabIndex 属性设置为负值来从选项卡顺序中删除 Web 服务器控件。You can remove a Web Server control from the tab order by setting the TabIndex property to a negative value.

备注

只有 Internet Explorer 4 及更高版本支持此属性。This property is supported only in Internet Explorer 4 and later.

适用于

另请参阅