how to set Focus on textBox in web page with c#

//focus function
public static void SetFocus(TextBox control)
        {
            StringBuilder sb = new StringBuilder();
                        
            sb.Append("\r\n<script language=’JavaScript’>\r\n");
            sb.Append("<!–\r\n");
            sb.Append("function SetFocus()\r\n");
            sb.Append("{\r\n");
            sb.Append("\tdocument.");
 
            Control p = control.Parent;
            while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
 
            sb.Append(p.ClientID);
            sb.Append("[‘");
            sb.Append(control.UniqueID);
            sb.Append("’].focus();\r\n");
            sb.Append("}\r\n");
            sb.Append("window.onload = SetFocus;\r\n");
            sb.Append("// –>\r\n");
            sb.Append("</script>");
 
            control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s