Setting Default Focus on ASP.NET Master Pages

In ASP.NET 2.0, the way to set focus on a particular form field is to add a defaultfocus declaration in the form tag.

<form id="Form1" defaultfocus="txtSearch" runat="server">

Since the form tag does not appear when using MasterPages, setting form focus is done in the code. This is the line that will accomplish the same focus in C#.

Page.Form.DefaultFocus = txtSearch.ClientID;
This entry was posted in ASP.NET and tagged , . Bookmark the permalink.

Comments are closed.