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

with TextBox1.Focus();

asp.net will gerenate the javascript for you.

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

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.

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.