RosanneParnell
asked on
Setting focus on a text box
How do I set focus on a text box in C#?
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.TextB ox
Just a sec...
Just a sec...
You can use some script like
document.forms["form-name" ].elements ["text-box -name"].fo cus();
in your asp file. you can have a function like
function setFocus()
{
document.forms["form-name" ].elements ["text-box -name"].fo cus();
}
See if this helps.
document.forms["form-name"
in your asp file. you can have a function like
function setFocus()
{
document.forms["form-name"
}
See if this helps.
That's javascript, btw
ASKER
Actually, I have a C# file as my codebehind because I'm using C#.NET. Can I use javascript here?
You should use a page.registerstartupscript so your form element names match.
have a look at my posts in this thread:
https://www.experts-exchange.com/questions/21163539/Hotmail-Style-Select-All-checkbox-on-a-asp-checkbox-need-little-help.html
or the article I linked to then
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIControlClassClientIDTopic.asp
have a look at my posts in this thread:
https://www.experts-exchange.com/questions/21163539/Hotmail-Style-Select-All-checkbox-on-a-asp-checkbox-need-little-help.html
or the article I linked to then
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIControlClassClientIDTopic.asp
whoops, meant this article which is much more in depth
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/ClientSideScript.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/ClientSideScript.asp
You can call javascript from your codebehind C# program as well.
One way is:
document.parentWindow.exec Script("ja vascriptst ring","Jav aScript");
where javascriptstring = "
function setFocus()
{
document.forms["form-name" ].elements ["text-box -name"].fo cus();
}
"
Make sure you replace the form name, textboxname, etc with your real values
One way is:
document.parentWindow.exec
where javascriptstring = "
function setFocus()
{
document.forms["form-name"
}
"
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)
}
RegisterClientScriptBlock(
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(
"<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
- Joe
ASKER
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>")
RegisterClientScriptBlock(
"<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
ASKER
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
I usually put it in my page.onload
ASKER
Okay, great - thanks!
Now when I added the call to setFocus, I got the following error:
C:\VSProjects\ManhattanApp s\Actuator DesignerSo lution\Act uatorDesig ner\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?
Now when I added the call to setFocus, I got the following error:
C:\VSProjects\ManhattanApp
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
- Joe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
- Joe
textBox1.Select()
The overloads allow you to specify a tab order or even text to select within the textbox