Solved

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

Posted on 2006-07-11
5
741 Views
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 :
GridView1.Rows[e.NewEditIndex].Cells[7].Focus();
but that didn't appear to do anything.
I guess I'm barking up the wrong tree. Any ideas?
0
Comment
Question by:cjmackenzie
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:valrog
ID: 17084591
0
 
LVL 3

Expert Comment

by:valrog
ID: 17084623
Try using

gridview1.focus

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

Author Comment

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

Accepted Solution

by:
valrog earned 250 total points
ID: 17084892
0
 

Author Comment

by:cjmackenzie
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];
            c.Focus();
            //Next line is another way of setting focus
            //this.SetFocus(c.ClientID);
        }
    }

Many thanks

Colin

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now