Home » Asp.net

DataGrid - Setting focus after a TextBox TextChanged() event.

Asp.NET v2.0

Data grid with mulitple texboxes per row.

Each textbox has a TextChange() event wired up for data validation.

These events fire just fine, the data validation works just fine.

My issue is this:  User enters text into TextBox1 one then tabs into TextBox2. The cursor appears for a split second inside of TextBox2 then disappears as focus is being set somewhere else on the page.  I'm not sure how that change of focus is happening.

In this scenario, after tabbing out of TextBox1, how can I maintain focus on TextBox2 (and then subsequently TextBox3, TextBox4,etc..)


5 Answers Found


Answer 1

If you're using the server-side TextChanged event, you can do it the following way:

        DataGridItem item = (DataGridItem)((TextBox)sender).NamingContainer;
        TextBox TextBox2 = (TextBox)item.FindControl("TextBox2");

That would be on the TextChanged event for TextBox1. On TextBox2 TextChanged event, find control TextBox3 etc.

for a gridview, change 'DataGridItem' to 'GridViewRow'.

*Sorry if you posted this in the javascript forum intentionally - thought I'd give it a try, since you mentioned you're using the TextChanged event.


Answer 2

Thanks for the reply.

I've tried this approach previously and it does not alter the behavior.

Upon tabbing out of TB1, the cursor appears briefly in TB2 then disappears.

I feel like the answer lies somewhere in the TextChanged() events and the the DataGrid re-binding.


Answer 3

I feel like the answer lies somewhere in the TextChanged() events and the the DataGrid re-binding.

I should have said before - make sure you're binding the DataGrid within the if (!IsPostBack) {} code block.


Answer 4

Thanks again for the reply.

I think you've hit the nail on the head.

I'm using a 3rd party, multi-file upload control.  I bind the Items of that control to the DataGrid, then hide the FileUploader.

I have to bind on the Form_PreRender() event.  


Answer 5

No, I think you don't need to use TextChanged to validate the control. in my mind I guess that you can use js instead of textChanged event's checking:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWebDemo.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

        function CheckTextValue(txtid, litid) {
            var txt = document.getElementById(txtid);
            if (txt.value == "") {
                alert("Not blank!");

    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                <asp:TemplateField HeaderText="Column1">
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        <asp:Literal ID="Literal1" runat="server"></asp:Literal>

                <asp:TemplateField HeaderText="Column2">
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

                <asp:TemplateField HeaderText="Column1">
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

Your code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWebDemo
    public partial class WebForm1 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                GridView1.DataSource = new string[] { "a", "b" };

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                TextBox txt1 = (TextBox)e.Row.FindControl("TextBox1");
                Literal lit = (Literal)e.Row.FindControl("Literal1");
                txt1.Attributes.Add("onblur", string.Format("CheckTextValue('{0}','{1}')",txt1.ClientID,lit.ClientID));

Then you can easily use Tab to move from one textbox to another...



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure