代码之家  ›  专栏  ›  技术社区  ›  a432511

ASP.NET C GridView选项卡索引问题

  •  2
  • a432511  · 技术社区  · 15 年前

    似乎在向ASP.NET网格视图添加行时,选项卡索引的行为不符合预期(或所需)。该选项卡不会在一行中的每一列之间切换,然后移动到下一行,而是将在一列中的每一行向下移动,然后移动到下一列,依此类推。简单地说,它将垂直而不是水平地制表。对于用户严重依赖键盘输入的数据输入应用程序来说,这可能是一个问题。

    这个问题有什么解决办法吗?

    3 回复  |  直到 15 年前
        1
  •  5
  •   a432511    15 年前

    我已经花了一段时间来解决这个问题了!希望它能帮助其他有同样问题的人。

    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;
    }
    
        2
  •  5
  •   Daniel Ballinger    15 年前

    看一看 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;
      }
    }
    
        3
  •  0
  •   Kelsey    15 年前

    可以在rowdatabound事件上手动为网格内的所有控件分配tabindex。计算出要在特定行上标记多少控件,然后根据行索引只需制定选项卡顺序。