ASP.NET C#GridView Tab索引问题

a43*_*511 2 c# asp.net gridview

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

针对这个问题的解决方案?

a43*_*511 6

我已经搞砸了一段时间并且有这个解决方案!希望它可以帮助其他有同样问题的人.

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;
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*ger 5

看看在转发器自动创建TabIndex

对于每个控件,您可以将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>
Run Code Online (Sandbox Code Playgroud)

然后在代码后面增加一个TabIndex计数器:

private int _tabIndex = 0;

public int TabIndex
{
  get
  {
    _tabIndex++;
    return _tabIndex;
  }
}
Run Code Online (Sandbox Code Playgroud)