【问题标题】:Set the TabIndex for a GridView boundfield header为 GridView boundfield header 设置 TabIndex
【发布时间】:2012-09-13 18:45:00
【问题描述】:

我正在尝试为网站设置 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 单元格切换后,列标题会获得焦点并且是可切换的,但这不是我想要的顺序。我认为这是因为在行单元格之后没有更多故意的 tabindexes 设置和默认页面 tabbing 启动并将焦点设置到没有设置 tabindex 的项目。

为了澄清,当前的tabindex如下:

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

我希望它是:

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

我整个上午都在尝试使用标记或代码来解决此问题,但似乎没有针对此问题的任何解决方案或论坛帖子。

谁能帮帮我?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案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());
                }                
            }
        }
    

    希望这对其他人有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多