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

屬性值

Int16

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.

範例

下列範例說明如何使用的 TabIndex 屬性 WebControl 來設定頁面上控制項的定位順序。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 Form 程式碼模型的詳細資訊,請參閱 ASP.NET Web Forms 頁面程式碼模型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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。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 Form 頁面上 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 Form 頁面上的控制項會根據每個控制項的屬性值,以遞增順序排列索引標籤 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 Form 頁面上宣告的順序來取得焦點。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.

適用於