473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
3 1522
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nitin | last post by:
Hi I have created function to check date and time. at the time of execution, if date is left empty the function returns the error message but then the focus goes to next field. Next filed is for...
5
by: tshad | last post by:
I have a date validation function that I want to stay at the object I am validating if there is a Validation error, but it always goes to the next object. The Javascript: function...
17
by: Neil Ginsberg | last post by:
OK, this is a stupid thing, but I can't seem to get this to work. I have a form with a subform (in continuous form view). A combo box on the main form has code in the AfterUpdate event which adds a...
8
by: copyco | last post by:
I want to be able to test if my app's form has focus, (ie: not in background, behind other windows). How can I do this? I tried the Me.Focused, but that doesn't work. I think it's because a...
2
by: Tom | last post by:
I am developing a VB.NET user control that is composed of a component control (along with some classes, etc). This is probably a stupid question, and should be easy, but I must be having a brain...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers for...
7
by: Dave Booker | last post by:
I am using a WebBrowser object in my .NET 2.0 application, but it is not shown to the user. Every time a timer event triggers it to perform a m_WebBrowser.Navigate() I get that classic IE 'click'...
4
by: Jon Slaughter | last post by:
I've created some custom controls and forms that allow the feature to temporarily transfer focus to a control that has been entered by the mouse. Everything seems to work fine but the problem I...
9
by: Zytan | last post by:
http://msdn2.microsoft.com/en-us/system.windows.forms.control.focus(VS.80).aspx this page says: "Focus is a low-level method intended primarily for custom control authors. Instead, application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.