Unable to set focus to a text box contained in a gridview's row cell after post-back and binding.
-
I have a gridview that contains rows where each row's cell contains a textbox. After inserting a new row into the gridview I want the new row's first textbox to have the focus. I have tried verious ways to set the new row first cell's textbox to have the focus but am unable. Does anyone know how this is done? Thanks, Steve
-
I have a gridview that contains rows where each row's cell contains a textbox. After inserting a new row into the gridview I want the new row's first textbox to have the focus. I have tried verious ways to set the new row first cell's textbox to have the focus but am unable. Does anyone know how this is done? Thanks, Steve
Send your .focus() code
-
I have a gridview that contains rows where each row's cell contains a textbox. After inserting a new row into the gridview I want the new row's first textbox to have the focus. I have tried verious ways to set the new row first cell's textbox to have the focus but am unable. Does anyone know how this is done? Thanks, Steve
My first thought would be to find the row that it is in Insert mode (
SelectedRow
maybe) and give it focus. My second thought would be to extend the GridView in a Server Control. Override theFocus()
method. In this method, if the GridView is in insert mode (GridView.CurrentMode
), then get the current row (SelectedRow
) and then set the focus to that row. If the GridView is not in insert mode than just pass the call to the base (base.Focus()).
-
Send your .focus() code
Here is the code I am trying:
protected void GridViewCollectionManagement_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowIndex == focus) { // TextBox tb = (TextBox)e.Row.Cells[0].Controls[1]; // this.SetFocus(tb); DefaultFocus = ((TextBox)e.Row.FindControl ("newCollectionName")).ClientID; }
One thing to note is that this gridview is contained in a ajax panel and is a content page of a master page.