C# 设置GridView边界字段标题的TabIndex

C# 设置GridView边界字段标题的TabIndex,c#,asp.net,gridview,C#,Asp.net,Gridview,我试图为站点设置tabindexes,但在gridview列标题方面遇到了问题。我可以为表单控件(使用标记)和gridview行单元格(使用C#)设置tabindex,但不能设置gridview列标题。以下是gridview标记: <asp:GridView ID="grdBCReferrals" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="grdBCReferrals_PageIndexChanging"

我试图为站点设置tabindexes,但在gridview列标题方面遇到了问题。我可以为表单控件(使用标记)和gridview行单元格(使用C#)设置tabindex,但不能设置gridview列标题。以下是gridview标记:

<asp:GridView ID="grdBCReferrals" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="grdBCReferrals_PageIndexChanging"
            OnSorting="grdBCReferrals_Sorting" HeaderStyle-CssClass="gridHeader" AllowPaging="True"
            AllowSorting="True" DataKeyNames="ID" Width="100%">                
    <Columns>            
        <asp:BoundField DataField="ID" HeaderText="Id" SortExpression="Id">
        </asp:BoundField>
        <asp:BoundField DataField="CreatedOn" HeaderText="Created On" SortExpression="CreatedOn">
        </asp:BoundField>
        <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type">
        </asp:BoundField>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name">
        </asp:BoundField>
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status">
        </asp:BoundField>            
    </Columns>
</asp:GridView>

我知道这是可能的,因为在通过所有gridview单元格进行选项卡切换后,列标题将获得焦点并可进行选项卡切换,但这不是我想要的顺序。我认为这是因为在行单元格之后,没有更多刻意设置的tabindex,默认的页面tabbing会启动,并将焦点设置为没有tabindex设置的项目

为了澄清,当前的索引如下:

表单控件>GridView单元格>GridView标题

我希望是:

表单控件>GridView标题>GridView单元格

我整个上午都在试图找到如何使用标记或代码隐藏来解决这个问题,但似乎没有任何解决方案或论坛帖子专门针对这个问题


有人能帮我吗?

在同事的帮助下,我终于解决了这个问题,我讨厌帖子没有更新时出现这种情况,因此:

使用以下代码为gridview添加OnRowDataBound触发器:

protected void grdBCReferrals_RowDataBound(object sender, GridViewRowEventArgs e)
    {            
        int LoopCounter;

        // Variable for starting index. Use this to make sure the tabindexes start at a higher
        // value than any other controls above the gridview. 
        // Header row indexes will be 110, 111, 112...
        // First data row will be 210, 211, 212... 
        // Second data row 310, 311, 312 .... and so on
        int tabIndexStart = 10; 

        for (LoopCounter = 0; LoopCounter < e.Row.Cells.Count; LoopCounter++)
        {                
            if (e.Row.RowType == DataControlRowType.Header)
            {
                // Check to see if the cell contains any controls
                if (e.Row.Cells[LoopCounter].Controls.Count > 0)
                {
                    // Set the TabIndex. Increment RowIndex by 2 because it starts at -1
                    ((LinkButton)e.Row.Cells[LoopCounter].Controls[0]).TabIndex = short.Parse((e.Row.RowIndex + 2).ToString() + tabIndexStart++.ToString());
                }
            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Set the TabIndex. Increment RowIndex by 2 because it starts at -1
                e.Row.Cells[LoopCounter].TabIndex = short.Parse((e.Row.RowIndex + 2).ToString() + tabIndexStart++.ToString());
            }                
        }
    }
protectedvoid grdBCReferrals\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{            
整数循环计数器;
//用于启动索引的变量。使用此变量可确保tabindexes以更高的值启动
//值,而不是gridview上的任何其他控件。
//标题行索引将是110、111、112。。。
//第一个数据行将是210、211、212。。。
//第二数据行310、311、312……等等
int tabIndexStart=10;
for(LoopCounter=0;LoopCounter0)
{
//将TabIndex.RowIndex设置为2,因为它从-1开始
((LinkButton)e.Row.Cells[LoopCounter].Controls[0]).TabIndex=short.Parse((e.Row.RowIndex+2.ToString()+tabIndexStart++.ToString());
}
}
else if(e.Row.RowType==DataControlRowType.DataRow)
{
//将TabIndex.RowIndex设置为2,因为它从-1开始
e、 Row.Cells[LoopCounter].TabIndex=short.Parse((e.Row.RowIndex+2).ToString()+tabIndexStart++.ToString());
}                
}
}
希望这对其他人有帮助