Dynamic Data – First text field focus


The Insert page template that is created for you when you create a new ASP.NET Dynamic Data Entities Web Application project, does not set focus to the first field in the template. By adding the following code (which can be simplified I’m sure) to the partial Insert class, you can achieve just that:

protected void Page_LoadComplete(object sender, EventArgs e)
{
    Text_EditField usrCtl = Utility.FindControl(FormView1.Controls);

    TextBox txtBox = (TextBox) usrCtl.FindControl("TextBox1");

    if (txtBox != null && !txtBox.ReadOnly)
        txtBox.Focus();
}

The static generic FindControl method was found here, http://erikporter.com/Blog/asp-net-findcontrol-recursive-with-generics.

Advertisements