Hi makumbi,
On TextChange event you need to set the RowIndex in a HiddenField.
Then using the HiddenField value next GridViewRow need to be referenced and call the focus function by referencing the TextBox.
Refer below example.
HTML
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:TextBox ID="txtId" runat="server" Text='<%#Eval("Id") %>' onkeyup="enter(this)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("Name") %>' onkeyup="enter(this)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%#Eval("Country") %>' onkeyup="enter(this)"
AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hfRowIndex" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function enter(obj) {
var tr = obj.parentNode.parentNode;
var cellIndex = obj.parentNode.cellIndex;
var rowIndex = obj.parentNode.parentNode.rowIndex;
if (event.keyCode == 37) //Left
{
if (cellIndex > 0)
tr.parentNode.rows[rowIndex].cells[cellIndex - 1].getElementsByTagName('INPUT')[0].focus();
return;
}
if (event.keyCode == 38) //Up
{
if (rowIndex > 1)
tr.parentNode.rows[rowIndex - 1].cells[cellIndex].getElementsByTagName('INPUT')[0].focus();
return;
}
if (event.keyCode == 39) //Right
{
if (cellIndex < tr.cells.length - 1)
tr.parentNode.rows[rowIndex].cells[cellIndex + 1].getElementsByTagName('INPUT')[0].focus();
return;
}
if (event.keyCode == 40) //Down
{
if (rowIndex < tr.parentNode.rows.length - 1)
tr.parentNode.rows[rowIndex + 1].cells[cellIndex].getElementsByTagName('INPUT')[0].focus();
return;
}
};
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
prm.add_endRequest(function (sender, e) {
if (sender._postBackSettings.panelsToUpdate != null) {
var index = parseInt(document.getElementById("hfRowIndex").value);
var row = document.getElementById("gvCustomers").rows[index + 2];
if (typeof (row) != "undefined") {
var controls = row.getElementsByTagName("INPUT");
for (var i = 0; i < controls.length; i++) {
if (controls[i].id.indexOf("txtCountry") != -1) {
// Focus to the next record.
controls[i].focus();
break;
}
}
}
}
});
};
</script>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void OnTextChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
hfRowIndex.Value = row.RowIndex.ToString();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Protected Sub txtCountry_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast(TryCast(sender, TextBox).NamingContainer, GridViewRow)
hfRowIndex.Value = row.RowIndex.ToString()
End Sub
Screenshot