Simple tips: Set Focus to TextBox in GridView (edit mode)

Posted by: Jotekes Blog, on 17 Feb 2007 | View original | Bookmarked: 0 time(s)

Replied on Forums:

If you use TemplateField like

<asp:TemplateField>
            <ItemTemplate>
                <%#Eval("EmailName") %>
            </ItemTemplate> 
            <EditItemTemplate>
                <asp:TextBox ID="txtEdit" runat="server" Text='<%#Eval("EmailName") %>' />
            </EditItemTemplate> 
        </asp:TemplateField>

 You could do it in RowEditing, for example:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //Normal turning to edit
            GridView1.EditIndex = e.NewEditIndex; 
            BindGrid();

            //Set the focus to control on the edited row
            GridView1.Rows[e.NewEditIndex].FindControl("txtEdit").Focus();
        }

 I'm binding the grid manually, therefore there is setting the index and calling databinding functions. However, with BoundField (I  have CommandField as left most column)  it could be something like:

GridView1.Rows[e.NewEditIndex].Cells[1].Controls[0].Focus(); 
 where Cells[index] is the one you should be modifying based on which column it is in question (still doing in RowEditing)
In case you use data-source controls, it might be feasible to do it in RowDataBound:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowState == DataControlRowState.Edit)
            {
                //Set the focus to control on the edited row
                e.Row.Cells[1].Controls[0].Focus();
                //Or to a specific control in TemplateField
               // e.Row.FindControl("txtEdit").Focus();
            }
        }
 

 

Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it!

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: GridView | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 12175 | Hits: 77

Similar Posts

  • VS 2010 Code Intellisense Improvements (VS 2010 and .NET 4.0 Series) more
  • On the Fly DropDown Editing with jQuery more
  • Silverlight Release History : Q2 2009 (version 2009.2.701) more
  • Sneak Peek: New ASP.NET Editors Features and Demo for 2009 volume 2! more
  • WPF Release History : Q1 2009 SP2 (version 2009.1.526) more
  • Silverlight Release History : Q1 2009 SP2 (version 2009.1.526) more
  • Convert Web.UI Grid into a data entry spreadsheet more
  • WPF Release History : Q1 2009 (version 2009.1.312) more
  • Silverlight Release History : Q1 2009 (version 2009.1.312) more
  • WebUI Test Studio Release History : Q1 2009 (version 2009.1.311) more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD