Another useful way to do this is to create a custom page base class that
will emit the necessary javascript into the client code and inherit your
pages from this class.
I have used the following code in one of my projects to achieve this goal:
using System;
using System.Text;
using System.Web.UI;
namespace WillcockConsulting.ProductCatalogue.UI.Web.Utils
{
public class ProductCatalogueWebPage : Page
{
protected void SetClientFocus(Control focusControl)
{
StringBuilder script = new StringBuilder(200, 200);
string clientID = focusControl.ClientID;
script.Append("<script language=\"javascript\">document.all.");
script.Append(clientID);
script.Append (".focus();</script>");
RegisterStartupScript("SetFocus", script.ToString());
}
}
}
Then you just need to inherit your page from ProductCatalogueWebPage (call
this anything you want of course) and call the SetClientFocus method passing
in your control - e.g.
SetClientFocus(textBox1);
Steve Willcock, MCSD
http://www.willcockconsulting.com/