How do I set focus to the first input field when I edit a row in GridView?

Posted on 2006-07-11
Last Modified: 2008-01-09
I want to set focus to the first input field in the row when the user selects "edit". I tried adding:
Control input1 = GridView1.Rows[e.NewEditIndex].Cells[7].Controls[0];
to the Rowediting event handler but this doesn't work because none of the Cells have controls.
So I tried :
but that didn't appear to do anything.
I guess I'm barking up the wrong tree. Any ideas?
Question by:cjmackenzie
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 17084623
Try using


This should set the focus to the first focasable cell inside that control.

Author Comment

ID: 17084805
I tried that in the RowEditing event handler and the PageLoad event handler without success. Any opther ideas?

Accepted Solution

valrog earned 250 total points
ID: 17084892

Author Comment

ID: 17097873
GridView1.Focus() doesn't work because my text box is not the first focussable cell - both the Sort headings and the Edit buttons can be focussed on too

But your link led me to the solution - I prefer the shorter C# solution to the longer VB solution containing javascript..
I've been trying to access the textbox control in the wrong event - should be on RowDataBinding:    

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState
        & DataControlRowState.Edit) != 0)
            Control c = e.Row.Cells[6].Controls[0];
            //Next line is another way of setting focus

Many thanks



Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question