467,075 Members | 1,089 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

set focus on the control

Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanks
Nov 18 '05 #1
  • viewed: 1400
Share:
3 Replies
Check out this code snippet,
http://www.extremeexperts.com/Net/Co...rPostback.aspx

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"Grey" <er*****@i-cable.com> wrote in message news:eq**************@TK2MSFTNGP12.phx.gbl...
Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanks
Nov 18 '05 #2
This is the code i use. it takes a control and sets the focus.
public static void SetInitialFocus(System.Web.UI.Control control)

{

if (control.Page == null)

{

throw new ArgumentException(

"The Control must be added to a Page before you can set the IntialFocus to it.");

}

if (control.Page.Request.Browser.JavaScript == true)

{

// Create JavaScript

System.Text.StringBuilder s = new System.Text.StringBuilder();

s.Append("\n<SCRIPT LANGUAGE='JavaScript'>\n");

s.Append("<!--\n");

s.Append("function SetInitialFocus()\n");

s.Append("{\n");

s.Append(" document.");

// Find the Form

System.Web.UI.Control p = control.Parent;

while (!(p is System.Web.UI.HtmlControls.HtmlForm))

p = p.Parent;

s.Append(p.ClientID);

s.Append("['");

s.Append(control.UniqueID);

// Set Focus on the selected item of a RadioButtonList

System.Web.UI.WebControls.TextBox rbl = control as System.Web.UI.WebControls.TextBox;

s.Append("'].focus();\n");

s.Append("}\n");

if (control.Page.SmartNavigation)

s.Append("window.setTimeout(SetInitialFocus, 500);\n");

else

s.Append("window.onload = SetInitialFocus;\n");

s.Append("// -->\n");

s.Append("</SCRIPT>");

// Register Client Script

control.Page.RegisterClientScriptBlock("InitialFoc us", s.ToString());

}

}
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Grey" <er*****@i-cable.com> wrote in message news:eq**************@TK2MSFTNGP12.phx.gbl...
Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanks
Nov 18 '05 #3
In code-behind file, register JavaScript code to handle client onclick method of submit button:

e.g. btnSubmit.Attributes.Add("onclick", "OnSubmitTest");

in .aspx file, add following JavaScript method:

function OnSubmitTest()
{
if (null != document.all("txtTextbox"))
document.all("txtTextbox").focus();
}

Tiendq,
ti**@tienonsoftware.com
"Grey" <er*****@i-cable.com> wrote in message news:eq**************@TK2MSFTNGP12.phx.gbl...
Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanks
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by tshad | last post: by
17 posts views Thread by Neil Ginsberg | last post: by
8 posts views Thread by copyco | last post: by
1 post views Thread by clickon | last post: by
7 posts views Thread by Dave Booker | last post: by
4 posts views Thread by Jon Slaughter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.