943,864 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 66741
  • C# RSS
Jul 12th, 2006
0

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

Expand 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...
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Gowrishankar is offline Offline
13 posts
since Jun 2006
Jul 12th, 2006
0

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

You would typically do that in the webform1.aspx page with javascript not in the WebForm1.asp.cs C# code-behind.

C# Syntax (Toggle Plain Text)
  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.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 12th, 2006
0

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

with TextBox1.Focus();

asp.net will gerenate the javascript for you.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Jul 12th, 2006
0

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

Hi,thanks.The first one is working....
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Gowrishankar is offline Offline
13 posts
since Jun 2006
Jul 12th, 2006
0

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

Quote ...
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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 12th, 2006
0

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

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]

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 14th, 2006
0

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

when you call .Focus() on a control in asp.net code. asp.net actually generates a seperate .js file.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Feb 28th, 2008
0

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

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>
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nukewarm is offline Offline
10 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Getting OUTPUT values back from SQL Stored Procedure
Next Thread in C# Forum Timeline: Auto Run Procedures in Windows Application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC