Mathew Nolton Blog

Software dementia unleashed...

Setting Focus in ASP.Net

I ran across this article in my search for setting focus to an individual field on page_load

The article was written by Roger McCook. Its real simple and it wouldn't have been hard to figure out, but since Roger figured it out for us, why waste any brain cycles.

protected void setFocus( System.Web.UI.Control ctrl )
{
     string s = "<SCRIPT language='javascript'>document.getElementById('" + ctrl.ID + "').focus() </SCRIPT>";
     this.RegisterStartupScript( "focus", s );
}

to use it you just do this.

this.setFocus( txUserId );

-Mathew Nolton

Posted: May 15 2003, 10:39 AM by MatiasN | with 29 comment(s)
Filed under:

Comments

Evil Donald said:

THis worked and totally rocks!
# August 12, 2003 12:26 AM

suhasini said:

can u please give me the same in asp.net using visual studio.
# January 14, 2004 2:23 AM

Mathew Nolton said:

Suhasini,
This is .net code. Just define the function setFocus() in your code behind class and then in your OnPageLoad event handler, call this.SetFocus( FieldToSetFocus );
HTH
-Mathew Nolton
# January 14, 2004 8:43 AM

asdf said:

dasfdfs
# January 24, 2004 10:12 AM

asasas said:

asasasasasasasasa
# February 18, 2004 10:32 AM

BaldArmy said:

I put this snippet in an inherited page class, changed ctrl.Id to ctrl.ClientId, and then was able to use it to locate controls inside UserControls. Very handy little script.
# March 3, 2004 2:55 PM

Orlando said:

I’ve got a page that has user controls (I used flow and not grid) and the user control that needs to have the focus set is the one handling the log in process.



I have changed the ctrl.Id to ctrl.ClientId as one reply suggested, but I do not know how to name the control in the calling line – “me.setfocus(me.txtUserName)” is not working.

Any help
# April 26, 2004 6:23 AM

Brad Scott said:

I have the same problem as Orlando. How can you reference a user control text box from the parent form?

# April 29, 2004 9:38 PM

Brad Scott said:

Orlando, I tried this as the parameter and it worked. Just substitute your user control name and field name.

setFocus(Me.FindControl("UcBatches1:txBatchHeaderCode"))


Cheers,

Brad
# April 29, 2004 10:25 PM

Tom said:

Yeh, but. You could do all this with one line of code before .net. (.focus).
# May 5, 2004 5:25 PM

Robin said:

I want to have the cursor automatically jump to the next textbox ctrl in a series of textbox ctrls, (i.e. SS#) When my users type in the first 3 numbers, the cursor jumps to the next textbox ctrl and so on. how can this be done?

A million thanks!
Robin
# May 26, 2004 8:32 AM

Robin said:

Oh, by the way, I coding in vb.net, not C#

thanks!
Robin
# May 26, 2004 8:33 AM

USA said:

Orlando,

Try this for VB.NET:

Public Sub setFocus(ByVal ctrl As System.Web.UI.Control)

Dim strS As String
strS = "<SCRIPT language='javascript'>document.getElementById('" + ctrl.ID + "').focus() </SCRIPT>"
Page.RegisterStartupScript("focus", strS)

End Sub
# May 27, 2004 3:23 PM

ajay said:

bravo! good work
# June 16, 2004 6:51 AM

abby said:

thank you so much! i have been looking for this for ages.
# June 18, 2004 9:05 AM

Lk said:

Great,Its working Fine...

Thnx

Cheers,
LK
# June 21, 2004 1:32 AM

Andreas said:

Does anyone know how to scroll the page down to the control being focused on? I have a long form with a DropDownList that does an AutoPostback. The drop down generally requires scrolling to reach, so after the postback, the drop down is off the page, even if I have set the focus to the drop down using the above script.
# June 24, 2004 12:23 PM

ds1892 said:

how if i got a problem like this:
i setfocus on textbox that locate at web form, then when a button on that form is clicked, a new pop up window will be shown, but it auto minized.y this happen?

how can i set the pop up window so that it wont auto minized//

thnks

ds1892
# July 4, 2004 10:15 PM

Barani said:

It is very very useful for me & it is simple to use.

Thanks
# July 6, 2004 10:21 AM

jack said:

I have the same problem as Andreas, any suggestions??
# July 12, 2004 8:23 PM

ulaga said:

pls let me know your answer on this
# July 19, 2004 10:33 AM

gk said:

Andre Andre

Its realy simple and handy code. I was hunting it from ages.
# July 27, 2004 8:08 AM

gk said:

Jak and Andreas when i used setFocus function with combo box my page scrolls down to corresponding combo . But if u want to save position of scrollbar in postback it can be done in asp.net with SmartNavigation attribute at Page directive. Add it like this--
<%@ Page Language="C#" ClassName="clsSampleMatrix" SmartNavigation=true %>

and use setFocus for combo. It should solve ur problem.
# July 27, 2004 8:51 AM

darl said:

I'm getting the error that login does not contain a definition for ReqisterStartupScript

# June 28, 2007 10:32 AM

wasim ahmed said:

thanks Mathew Nolton it works

# September 4, 2007 5:42 AM

Danial said:

Working perfectly thanx :)

# September 7, 2007 11:06 AM

JB Burayag said:

Hey...ive got a problem with my control which to autopostback="true"...

Please Help Me..

# October 7, 2007 4:56 AM

JB Burayag said:

Its doesnt work with control that has autopostback set to true...please help..thank you very much...

# October 7, 2007 4:59 AM

M.Manmadha Rao said:

It works with web controls, but do not work with mobile web controls in mobile web form in ASP.NET. I shall be happy to have the code in VB.NET

# January 5, 2008 10:09 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)