Wednesday 17 September 2014

How to move cursor from left to right and right to left same as top to bottom and bottom to top on GridView Textboxes in C#.NET , ASP.NET.

In this article I will explain How to move cursor from left to right and right to left same as top to bottom and bottom to top on GridView Textboxes in C#.NET , ASP.NET , JavaScript.

(OR)

GridView Textboxes cursor moving from left to right and right to left same as top to bottom and bottom to top in C#.NET , ASP.NET , JavaScript.

(OR)

Left to right and right to left same as top to bottom and bottom to top cursor moving in GridView Textboxes C#.NET , ASP.NET , JavaScript.

    
  First we have to design the GridView page. Then go to GridView properties, double click on DataBound event. Copy the below code and paste it in to your c# page.


protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow gvrprev;
        GridViewRow gvrnext;

        TextBox tbLeft;
        TextBox tbDown;
        TextBox tbRight;
        TextBox tbUp;

        int RowID;

        foreach (GridViewRow r in GridView1.Rows)
        {
            RowID = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value.ToString());

            if (r.RowIndex == 0)
            {
                gvrprev = r;
            }
            else
            {
                gvrprev = GridView1.Rows[r.RowIndex - 1];
            }

            if (r.RowIndex == GridView1.Rows.Count - 1)
            {
                gvrnext = r;
            }
            else
            {
                gvrnext = GridView1.Rows[r.RowIndex + 1];
            }

            for (int i = 2; i < 5; i++)
            {
                TextBox tbMarks = (TextBox)r.Cells[i].Controls[1];


                if (i == 2)
                {
                    tbLeft = tbMarks;
                }
                else
                {
                    tbLeft = (TextBox)r.Cells[i - 1].Controls[1];
                }
                if (i == 4)
                {
                    tbRight = tbMarks;
                }
                else
                {
                    tbRight = (TextBox)r.Cells[i + 1].Controls[1];
                }

                tbUp = (TextBox)gvrprev.Cells[i].Controls[1];
                tbDown = (TextBox)gvrnext.Cells[i].Controls[1];

                tbMarks.Attributes.Add("onKeydown", "arrowkeychange('" + tbMarks.ClientID + "','" + tbLeft.ClientID + "','" + tbDown.ClientID + "','" + tbRight.ClientID + "','" + tbUp.ClientID + "');");
                //tbMarks.Attributes.Add("onFocus", "Focus('" + tbMarks.ClientID + "');");

            }
        }
    }

Below script code is useful for Identify the keys of Left, Right, top, bottom keys in Keyboard.

<script type="text/javascript" language="javascript">

        function arrowkeychange(txtCurrent, txtLeft, txtDown, txtRight, txtUp) {

            var key = event.keyCode;

            //Up Key Pressed
            if (key == 38) {

                document.getElementById(txtUp).focus();
                return false;
            }

            //Left Key
            if (key == 37) {
                document.getElementById(txtLeft).focus();
                return false;
            }

            //Down or Enter Keys
            if (key == 40 || key == 13) {
                document.getElementById(txtDown).focus();
                return false;
            }

            //Right Arrow Key
            if (key == 39) {
                document.getElementById(txtRight).focus();
                return false;
            }

        }

</script>

No comments:

Post a Comment