How to set the focus on a text box..?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 13
Reputation: Gowrishankar is an unknown quantity at this point 
Solved Threads: 1
Gowrishankar Gowrishankar is offline Offline
Newbie Poster

How to set the focus on a text box..?

 
0
  #1
Jul 12th, 2006
Hi,
How to set the focus in a text box control?I want to blink the cursor in the textbox control while executing...Is there any property for this..?What is the command in ASP.NET using C#..?Thanks in advance...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How to set the focus on a text box..?

 
0
  #2
Jul 12th, 2006
You would typically do that in the webform1.aspx page with javascript not in the WebForm1.asp.cs C# code-behind.

  1. <script type="text/javascript">
  2. document.forms[0]["TextBox1"].focus();
  3. </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.

  1. <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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: How to set the focus on a text box..?

 
0
  #3
Jul 12th, 2006
with TextBox1.Focus();

asp.net will gerenate the javascript for you.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 13
Reputation: Gowrishankar is an unknown quantity at this point 
Solved Threads: 1
Gowrishankar Gowrishankar is offline Offline
Newbie Poster

Re: How to set the focus on a text box..?

 
0
  #4
Jul 12th, 2006
Hi,thanks.The first one is working....
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How to set the focus on a text box..?

 
0
  #5
Jul 12th, 2006
asp.net will gerenate the javascript for you.
In .NET 2.0 it will yes, but it's yucky IMHO and relies on the client scripts being installed properly in IIS and parent paths. Use it in an isolated project so you can see exactly what it does, then you can use it safely.

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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How to set the focus on a text box..?

 
0
  #6
Jul 12th, 2006
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]

  1. addLoadListener(myinit);
  2.  
  3. function myinit()
  4. {
  5. //Initialise stuff goes here
  6. document.forms[0]["TextBox1"].focus();
  7. }
  8.  
  9. function addLoadListener(fn)
  10. {
  11. if (typeof window.addEventListener != 'undefined')
  12. {
  13. window.addEventListener('load', fn, false);
  14. }
  15. else if (typeof document.addEventListener != 'undefined')
  16. {
  17. document.addEventListener('load', fn, false);
  18. }
  19. else if (typeof window.attachEvent != 'undefined')
  20. {
  21. window.attachEvent('onload', fn);
  22. }
  23. else
  24. {
  25. var oldfn = window.onload;
  26. if (typeof window.onload != 'function')
  27. {
  28. window.onload = fn;
  29. }
  30. else
  31. {
  32. window.onload = function()
  33. {
  34. oldfn();
  35. fn();
  36. };
  37. }
  38. }
  39. }

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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: How to set the focus on a text box..?

 
0
  #7
Jul 14th, 2006
when you call .Focus() on a control in asp.net code. asp.net actually generates a seperate .js file.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 10
Reputation: nukewarm is an unknown quantity at this point 
Solved Threads: 0
nukewarm nukewarm is offline Offline
Newbie Poster

Re: How to set the focus on a text box..?

 
0
  #8
Feb 28th, 2008
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.
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        SetFocus(Login1.FindControl("UserName"));
    }
    </script>
Originally Posted by Gowrishankar View Post
Hi,
How to set the focus in a text box control?I want to blink the cursor in the textbox control while executing...Is there any property for this..?What is the command in ASP.NET using C#..?Thanks in advance...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC