Link to home
Start Free TrialLog in
Avatar of RosanneParnell
RosanneParnell

asked on

Setting focus on a text box

How do I set focus on a text box in C#?
Avatar of aacool
aacool

Use

textBox1.Select()

The overloads allow you to specify a tab order or even text to select within the textbox
Avatar of RosanneParnell

ASKER

I got the following error: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Select'
Ah - you are using ASP.NET - The method is available only with System.Windows.Forms.TextBox

Just a sec...
You can use some script like

document.forms["form-name"].elements["text-box-name"].focus();

in your asp file. you can have a function like
function setFocus()
{
    document.forms["form-name"].elements["text-box-name"].focus();
}


See if this helps.
That's javascript, btw
Actually, I have a C# file as my codebehind because I'm using C#.NET. Can I use javascript here?
You can call javascript from your codebehind C# program as well.

One way is:

document.parentWindow.execScript("javascriptstring","JavaScript");

where javascriptstring = "
function setFocus()
{
    document.forms["form-name"].elements["text-box-name"].focus();
}
"

Make sure you replace the form name, textboxname, etc with your real values
Actually, use the following

RegisterClientScriptBlock(key, script)
The RegisterClientScriptBlock method adds a block of client-side script after the Web Form's rendered <form> element, before any Web controls contained within the Web Form. The key input parameter allows you to specify a unique key associated with this script block, whereas the script parameter includes the complete script code to emit. (This script parameter should include the actual <script> element, along with the client-side JavaScript or Microsoft VBScript.)

e.g.
    RegisterClientScriptBlock("setFocus-function", _
            "<script language=""JavaScript"">" & vbCrLf & _
            "function setFocus(id) {" & _
            "  var o = document.getElementById(id); " & _
            "if (o != null) o.focus(); " & _
            "}" & vbCrLf & _
            "</script>")

To call this from your C# codebehind file:

private void SetFocusBtnClicK(object sender, System.EventArgs e)
{
  setFocus(textBox1)
}
The question has been anwsered, though just a tip.  Create your own custom Page, that inherits from the .NET page class.  Then include this focus script as a property or function available to that class.  Then have all of your pages inherit from your custom class, instead of the default .NET class.  That way this functionality is available to all your pages if you ever need it, and don't have to register the script each time.  It's real handy.  I've made ones for focusing on controls, setting the title, and poping up new windows sending dynamic parameters.  They're slick and easy to use.


 - Joe
Where does the javascript reside.

   RegisterClientScriptBlock("setFocus-function", _
            "<script language=""JavaScript"">" & vbCrLf & _
            "function setFocus(id) {" & _
            "  var o = document.getElementById(id); " & _
            "if (o != null) o.focus(); " & _
            "}" & vbCrLf & _
            "</script>")
that piece of code will inject your script in the html source, try it and do a view source on the resulting page
No, I'm asking where do I put the RegisterClientScriptBlock paragraph? In the codebehind page? In the HTML of the .aspx page?
in the cs file (codebehind)
I usually put it in my page.onload
Okay, great - thanks!

Now when I added the call to setFocus, I got the following error:

C:\VSProjects\ManhattanApps\ActuatorDesignerSolution\ActuatorDesigner\Login.aspx.cs(65): The name 'setFocus' does not exist in the class or namespace 'Actuator1.Login'


I placed it right after the RegisterClientScriptBlock paragraph in Page_Load. What am I doing wrong here?
Can you post your relevant functions?
I'm working up a small example to show how to register Javascript in a Custom Class, and use that class on a Page.  Functions will include Focus, Title, and PopUp.


 - Joe
ASKER CERTIFIED SOLUTION
Avatar of Realmrat
Realmrat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'd gladly take the points.  Registering the javascript functions per page does work, but I think the idea of building upon a base page with base functionality it the best way to go.  =]

- Joe