In Master we should add the below code,

<ajaxtoolkit:toolkitscriptmanager id="smLoginMaster" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/Script/jquery-1.3.2.js" />
        </Scripts>
</ajaxtoolkit:toolkitscriptmanager>

download the file jquery-1.3.2.js from http://docs.jquery.com/Downloading_jQuery

In the page load event, add the below code,

ScriptManager.RegisterStartupScript(ControlID.Page, _ ControlID.GetType(), ControlID.ClientID + "_SetFocusOnLoad", CommonBehavior.SetFocusScript(Control), true);

Example :

ScriptManager.RegisterStartupScript(_txtFirstname.Page, _txtFirstname.GetType(), _txtFirstname.ClientID + "_SetFocusOnLoad", CommonBehavior.SetFocusScript(_txtFirstname), true);

And write a common function for get the Jquery script like below,

/// <summary>
/// Get the Focus script
/// </summary>
/// <param name="Control">Focus Control ID</param>
/// <returns>returns the javascript string</returns>
public static string SetFocusScript(Control Control)
{
   return "(function() { " + "var fn = function() { " + "var control = $get('" + Control.ClientID + "'); " + "if (control && control.focus) { control.focus(); } " + "Sys.Application.remove_load(fn);" + "};" + "Sys.Application.add_load(fn);" + "})();";
}


Comments (0)