Solved

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

Posted on 2006-07-11
5
738 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

19 Experts available now in Live!

Get 1:1 Help Now