2009-11-19 71 views
2

看起來,向ASP.NET GridView添加一行時,選項卡索引的行爲並不像期望的(或期望的)。而不是在一行中的每一列上標籤,然後移動到下一行,該標籤將向下移動一列中的每一行,然後移動到下一列,依此類推。簡而言之,它將垂直選擇而不是水平。對於用戶嚴重依賴鍵盤輸入的數據輸入應用來說,這可能是一個問題。ASP.NET C#GridView選項卡索引問題

此問題的任何解決方案?

回答

5

我一直在使用這一段時間,並有這個解決方案!希望它能幫助其他有相同問題的人。

protected void theGridView_DataBound(object sender, EventArgs e) 
{ 
    SetTabIndexes(); 
} 


private void SetTabIndexes() 
{ 
    short currentTabIndex = 0; 
    inputFieldBeforeGridView.TabIndex = ++currentTabIndex; 

    foreach (GridViewRow gvr in theGridView.Rows) 
    { 
     DropDownList dropDown1 = (DropDownList)gvr.FindControl("dropDown1"); 
     dropDown1.TabIndex = ++currentTabIndex; 

     TextBox inputField1 = (TextBox)gvr.FindControl("inputField1"); 
     inputField1.TabIndex = ++currentTabIndex; 

     TextBox inputField2 = (TextBox)gvr.FindControl("inputField2"); 
     inputField2.TabIndex = ++currentTabIndex; 

     TextBox inputField3 = (TextBox)gvr.FindControl("inputField3"); 
     inputField3.TabIndex = ++currentTabIndex; 
    } 

    someLinkAfterGridView.TabIndex = ++currentTabIndex; 
} 
0

您可以在rowdatabound事件中手動爲網格內的所有控件分配TabIndex。找出想要在特定行上選擇多少個控件,然後根據行索引制定Tab鍵順序。

+0

正好!對於那些可能正在谷歌搜索這個問題的人來說,這更是一個問題。我在那裏找不到任何東西。所以在這裏! – a432511 2009-11-19 16:43:48

5

看一看Automatically create TabIndex in a repeater

對於您可以在代碼中的TabIndex設置爲一個屬性後面每個控件。

代碼
<asp:GridView ID="gv" runat="server"> 
    <columns> 
    <asp:TemplateField HeaderText="Action" ShowHeader="False" Visible="true"> 
     <ItemTemplate> 
     <asp:CheckBox ID="cbGroup" Checked="true" runat="server" TabIndex='<%# TabIndex %>' Text='<%# Eval("Title") %>' /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    </columns> 
</asp:GridVeiw> 

那麼後面增量的TabIndex計數器:

private int _tabIndex = 0; 

public int TabIndex 
{ 
    get 
    { 
    _tabIndex++; 
    return _tabIndex; 
    } 
} 
+0

+1乾淨優雅的解決方案,感謝您對原作者的評價。 – 2011-06-01 00:10:36