为GridView boundfield标头设置TabIndex

时间:2012-09-21 10:37:49

标签: c# asp.net gridview

我正在尝试设置网站的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并且默认页面选项卡启动并将焦点设置为没有设置tabindex的项目。

澄清一下,目前的tabindex如下:

表单控件&gt; GridView Cells&gt; Gridview标题

我希望它是:

表单控件&gt; GridView标题&gt; GridView单元格

我一直试图找出如何在整个上午使用标记或代码来执行此操作,但似乎没有针对此问题的任何解决方案或论坛帖子。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我设法最终在同事的帮助下解决了这个问题,我讨厌在帖子没有更新的时候,所以这里有:

使用以下代码为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());
            }                
        }
    }

希望这有助于其他人