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

25 Comments

  • THis worked and totally rocks!

  • can u please give me the same in asp.net using visual studio.

  • 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

  • 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.

  • 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

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



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



    setFocus(Me.FindControl(&quot;UcBatches1:txBatchHeaderCode&quot;))





    Cheers,



    Brad

  • Yeh, but. You could do all this with one line of code before .net. (.focus).

  • 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

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



    thanks!

    Robin

  • Orlando,



    Try this for VB.NET:



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



    Dim strS As String

    strS = &quot;&lt;SCRIPT language='javascript'&gt;document.getElementById('&quot; + ctrl.ID + &quot;').focus() &lt;/SCRIPT&gt;&quot;

    Page.RegisterStartupScript(&quot;focus&quot;, strS)



    End Sub

  • thank you so much! i have been looking for this for ages.

  • Great,Its working Fine...



    Thnx



    Cheers,

    LK

  • 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.

  • 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

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



    Thanks

  • I have the same problem as Andreas, any suggestions??

  • pls let me know your answer on this

  • Andre Andre



    Its realy simple and handy code. I was hunting it from ages.

  • 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--

    &lt;%@ Page Language=&quot;C#&quot; ClassName=&quot;clsSampleMatrix&quot; SmartNavigation=true %&gt;



    and use setFocus for combo. It should solve ur problem.

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

  • thanks Mathew Nolton it works

  • Working perfectly thanx :)

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

  • 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

Comments have been disabled for this content.