WebControl.TabIndex 屬性

定義

取得或設定 Web 伺服器控制項的定位索引。

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 伺服器控制項的定位索引。 預設值為 0,表示這個屬性尚未設定。

例外狀況

指定的定位索引不在 -32768 和 32767 之間。

範例

下列範例說明如何使用 TabIndexWebControl 屬性來設定頁面上控制項的定位順序。

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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頁面上網頁伺服器控制項的索引標籤索引。 當您按下按鍵時 Tab ,Web 服務器控制項接收焦點的順序取決於 TabIndex 每個控制項的 屬性。 一開始載入頁面時,按下按鍵時 Tab 接收焦點的第一個專案就是網址列。 接下來,Web Form頁面上的控制項會根據每個控制項的 TabIndex 屬性值,以最小正數、非零值開始,以遞增順序排列為 。 如果多個控制項共用相同的索引標籤索引,控制項會依宣告在Web Form頁面上的順序接收焦點。 最後,索引標籤索引為零的控制項會依宣告的順序,定位到 。

注意

只有具有非零索引索引的控制項才會呈現 tabindex 屬性。

您可以將 屬性設定 TabIndex 為負值,以從定位順序移除 Web 服務器控制項。

注意

此屬性僅支援 Internet Explorer 4 和更新版本。

適用於

另請參閱