1,105,295 Community Members

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

Member Avatar
Gowrishankar
Newbie Poster
13 posts since Jun 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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...

Member Avatar
hollystyles
Veteran Poster
1,179 posts since Feb 2005
Reputation Points: 113 [?]
Q&As Helped to Solve: 78 [?]
Skill Endorsements: 0 [?]
 
0
 

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

<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.

<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.

Member Avatar
plazmo
Posting Whiz in Training
206 posts since Aug 2005
Reputation Points: 1 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

with TextBox1.Focus();

asp.net will gerenate the javascript for you.

Member Avatar
Gowrishankar
Newbie Poster
13 posts since Jun 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,thanks.The first one is working....

Member Avatar
hollystyles
Veteran Poster
1,179 posts since Feb 2005
Reputation Points: 113 [?]
Q&As Helped to Solve: 78 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
hollystyles
Veteran Poster
1,179 posts since Feb 2005
Reputation Points: 113 [?]
Q&As Helped to Solve: 78 [?]
Skill Endorsements: 0 [?]
 
0
 

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

<script type="text/javascript" src="scripts/project_name_here.js"></script>
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.

Member Avatar
plazmo
Posting Whiz in Training
206 posts since Aug 2005
Reputation Points: 1 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

when you call .Focus() on a control in asp.net code. asp.net actually generates a seperate .js file.

Member Avatar
nukewarm
Newbie Poster
10 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

<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...

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article