Home All Groups Group Topic Archive Search About
Author
7 Mar 2006 8:04 PM
rlrcstr
I've got a gridview all setup so that you can edit the items.  But how can I
force the first textbox in the editing template to have focus when the page
is displayed?  The name of the control is generated dynamically based on the
number of rows and controls in the grid.  Anyone done this yet?  Thanks.

Jerry

Author
8 Mar 2006 7:21 AM
Steven Cheng[MSFT]
Hi Jerry,

For the question on setting focus for the the first textbox in the edit
template of the GridView control, here are some of my suggestion:

1. If you can consider using TemlateFields, it'll be convenient to do it.
We can assign the TextBox in the first data column a fixed ID and use the
Control.FindControl to reference to the TextBox in the GridView's
RowCreated event and set the focus, e.g:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex
== GridView1.EditIndex)
        {
                       Page.SetFocus(e.Row.FindControl("txtName")); 
        }
    }


2. If you have to use boundfields, the textbox(for edit mode) is assigned a
random id, we could only try locating it through control index in the
parent collection ( we need to determine this index depend on the column's
order and postion in the gridview). e.g:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex
== GridView1.EditIndex)
        {
            Response.Write("<br/>" + e.Row.Cells[1].Controls[0]);
            Page.SetFocus(e.Row.Cells[1].Controls[0]); 
        }
    }

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)