Change Tab order of controls inside Gridview

Change Tab order of controls inside Gridview

Normally tab order for controls inside Gridview is always next from its previous control .
But in some scenario if you want to change tab order according to your requirement,you can use this approach.
 

Step 1: Design Web form .aspx page

<div>
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false"
            BorderWidth="0" GridLines="None" CssClass="w100p grid form" OnRowDataBound="gv_RowDataBound"      AlternatingRowStyle-BackColor="WhiteSmoke">
            <HeaderStyle BackColor="#3366ff" Font-Bold="true" ForeColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="#">
                    <ItemTemplate>
                        <%#Container.DataItemIndex+1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:TextBox ID="txtDbaName" runat="server" MaxLength="100" placeholder="First Name"></asp:TextBox>
                        <asp:HiddenField ID="hdAccountId" Value='<%# Bind("Name") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:TextBox ID="txtLegalName" runat="server" MaxLength="100" placeholder="Last Name"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Enrolment ID">
                    <ItemTemplate>
                        <asp:TextBox ID="txtTaxId" runat="server" MaxLength="9" placeholder="Enrolment ID"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAddress" runat="server" placeholder="Address" MaxLength="100"
                            ToolTip="Address"></asp:TextBox>&nbsp;
                        <asp:TextBox ID="txtPhone" placeholder="Phone" class="small" runat="server" ToolTip="Phone"></asp:TextBox>
                        <br />
                        <asp:TextBox ID="txtCity" runat="server" placeholder="City" MaxLength="50" ToolTip="City"></asp:TextBox>&nbsp;
                        <asp:DropDownList ID="drdState" runat="server" class="CustomDropDownSmallActivation">
                        </asp:DropDownList>
                        &nbsp;<asp:TextBox ID="txtZip" runat="server" class="Zip" placeholder="Zip" MaxLength="10"
                            ToolTip="Zip"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>



Step 2: Logic on .Cs page

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        { Bind_Gr(); }

    }

    public void Bind_Gr()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");

        dt.Rows.Add("Kandy");
        dt.Rows.Add("Sam");
        dt.Rows.Add("Izhar");
        dt.Rows.Add("Vineet");

        gv.DataSource = dt;
        gv.DataBind();
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                #region "Set Tabing"
                DropDownList drdState = e.Row.FindControl("drdState") as DropDownList;
                HiddenField hdStateId = e.Row.FindControl("hdStateId") as HiddenField;
                TextBox dbaName = e.Row.FindControl("txtDbaName") as TextBox;
                TextBox txtDbaName = (TextBox)e.Row.FindControl("txtDbaName");
                TextBox txtLegalName = (TextBox)e.Row.FindControl("txtLegalName");
                TextBox txtTaxId = (TextBox)e.Row.FindControl("txtTaxId");
                TextBox txtAddress = (TextBox)e.Row.FindControl("txtAddress");
                TextBox txtPhone = (TextBox)e.Row.FindControl("txtPhone");
                TextBox txtCity = (TextBox)e.Row.FindControl("txtCity");
                TextBox txtZip = (TextBox)e.Row.FindControl("txtZip");

                int i = e.Row.RowIndex * 8;// here 8 is number of control

                txtDbaName.Attributes.Add("tabindex", (i + 1).ToString());
                txtLegalName.Attributes.Add("tabindex", (i + 2).ToString());
                txtTaxId.Attributes.Add("tabindex", (i + 3).ToString());
                txtAddress.Attributes.Add("tabindex", (i + 4).ToString());
                txtCity.Attributes.Add("tabindex", (i + 5).ToString());
                drdState.Attributes.Add("tabindex", (i + 6).ToString());
                txtZip.Attributes.Add("tabindex", (i + 7).ToString());
                txtPhone.Attributes.Add("tabindex", (i + 8).ToString());

                if (e.Row.RowIndex == 0)
                    dbaName.Focus();

                #endregion "Set Tabing"



            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
    }



It will look like :-




Comments

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click