| | |
How to set the focus on a text box..?
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
You would typically do that in the webform1.aspx page with javascript not in the WebForm1.asp.cs C# code-behind.
You need to put the javascript at the bottom of the aspx, or make as the onload event of the body tag e.g.
This is to ensure TextBox1 has been rendered by the browser before you try to set focus on it, otherwise you get a null reference error.
C# Syntax (Toggle Plain Text)
<script type="text/javascript"> document.forms[0]["TextBox1"].focus(); </script>
You need to put the javascript at the bottom of the aspx, or make as the onload event of the body tag e.g.
C# Syntax (Toggle Plain Text)
<body onload="javacript:document.forms[0]["TextBox1"].focus();">
This is to ensure TextBox1 has been rendered by the browser before you try to set focus on it, otherwise you get a null reference error.
•
•
•
•
asp.net will gerenate the javascript for you.
I always recommend this kind of thing should be done with an init() javascript function in a .js file, and use W3C event handlers to call init() from the browsers onload event, falling back to the old <body onload="init()"> as a last resort
javascript parsing and execution is asynchronous and behave differently across browsers. This is the only way to be sure you users don't get nasty script errors from race conditions caused by snippets of javascript all over the place.
Last edited by hollystyles; Jul 12th, 2006 at 11:52 am.
heres how I always initialise my java stuff using a handy cross browser complient function for settign the load event handler that I got from Sitepoint. I generaly call this project_name_here.js and include it in the <head></head> tags of my aspx like this
[HTML] <script type="text/javascript" src="scripts/project_name_here.js"></script>[/HTML]
this ensures all the javascript is parsed, and all the DOM is parsed and rendered and you can start executing your script safely.
[HTML] <script type="text/javascript" src="scripts/project_name_here.js"></script>[/HTML]
C# Syntax (Toggle Plain Text)
addLoadListener(myinit); function myinit() { //Initialise stuff goes here document.forms[0]["TextBox1"].focus(); } function addLoadListener(fn) { if (typeof window.addEventListener != 'undefined') { window.addEventListener('load', fn, false); } else if (typeof document.addEventListener != 'undefined') { document.addEventListener('load', fn, false); } else if (typeof window.attachEvent != 'undefined') { window.attachEvent('onload', fn); } else { var oldfn = window.onload; if (typeof window.onload != 'function') { window.onload = fn; } else { window.onload = function() { oldfn(); fn(); }; } } }
this ensures all the javascript is parsed, and all the DOM is parsed and rendered and you can start executing your script safely.
Last edited by hollystyles; Jul 12th, 2006 at 12:19 pm.
•
•
Join Date: Feb 2008
Posts: 10
Reputation:
Solved Threads: 0
Forget about trying to use javascript in these asp pages. The better you get at c# the more you can fully use the .Nets.
This should work. Insert code right above the <html...> tag.
This should work. Insert code right above the <html...> tag.
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
SetFocus(Login1.FindControl("UserName"));
}
</script>![]() |
Similar Threads
- pass the value to the text box (JavaScript / DHTML / AJAX)
- Setting the focus to a specific text box and other woes (C#)
- Text Box Newbie help (Visual Basic 4 / 5 / 6)
- Text box population from DBgrid. HELP!! (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: Getting OUTPUT values back from SQL Stored Procedure
- Next Thread: Auto Run Procedures in Windows Application
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot math mouseclick mp3 mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string study table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






