Why TextBox1.Focus doesnt work in ASP .NET?? what code should I write on this?
Printable View
Why TextBox1.Focus doesnt work in ASP .NET?? what code should I write on this?
Are you trying to use that in the code behind? If so, the .NET code will run on the server, and you need to set focus to controls from the client.
Try using javascript.
This may help you. Vb.net code.
Code:Sub Set_Focus(ByVal Control As TextBox)
Dim strScript As String
strScript = "<script language=javascript> document.all('" & Control.ID & "').focus() </script>"
RegisterStartupScript("focus", strScript)
End Sub
This code works for my dropdownlists, but not my textboxes. Any ideas?
FYI:
VB Code:
Private Sub Set_Focus(ByVal oControl As Control) Dim strScript As String strScript = "<script language=javascript> document.all('" & oControl.ID & "').focus() </script>" RegisterStartupScript("ClientSideScript", strScript) End Sub
This works fine indead, but what if you want that all the text of your textbox is selected ?
I tried to change it as :
VB Code:
strScript = "<script language=javascript> document.all('" & oControl.ID & "').select() </script>"
This works, but when you push the tabbutton, the focus goes to the addressbar of IE ?
Any idea/solution ?
Have you set the tabindex property for the textbox?
No, all tabindexes are set to 0.
Change them so your lowest index is 1.
Out of interest, "document.all" is IE specific; it will not work in other browsers. Rather use document.getElementById instead.
Sorry, but this doesn't help, even with indexes and "document.getElementById ". :cry:
Here's my logon screen :
Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Login</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="Profortra.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form name="Form1" method="post" action="Login.aspx?LogoffFirst=Yes" language="javascript" onsubmit="if (!ValidatorOnSubmit()) return false;" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwxNzg4MjI2ODk7dDw7bDxpPDE+Oz47bDx0PDtsPGk8MTE+Oz47bDx0PHA8cDxsPFRleHQ7PjtsPFdyb25nIGNyZWRlbnRpYWxzLiBQbGVhc2UgdHJ5IGFnYWluLjs+Pjs+Ozs+Oz4+Oz4+O2w8UGVyc2lzdDs+PlKKdaZvT11aLYLYLFpQUZrbrIPc" />
<script language="javascript" type="text/javascript" src="/aspnet_client/system_web/1_1_4322/WebUIValidation.js"></script>
<H1 align="left"><FONT size="7"><IMG src="Images/PROFORTRA.jpg"></FONT></H1>
<H1 align="left"><FONT size="7">Welcome</FONT></H1>
<H2 align="left">To access this site you have to log on first.</H2>
<P>
<TABLE class="TABLE" id="tlbLogin" style="WIDTH: 560px; HEIGHT: 130px" cellSpacing="1"
cellPadding="1" width="560" border="0">
<TR>
<TD style="WIDTH: 91px">Username
</TD>
<TD><input name="UserName" type="text" value="TEST" id="UserName" tabindex="1" style="background-color:LightGrey;width:160px;" /> <span id="reqUserName" controltovalidate="UserName" errormessage="Username is required !" evaluationfunction="RequiredFieldValidatorEvaluateIsValid" initialvalue="" style="color:Red;visibility:hidden;">Username is required !</span></TD>
</TR>
<TR>
<TD style="WIDTH: 91px">Password</TD>
<TD><input name="PassWord" type="password" id="PassWord" tabindex="2" style="background-color:LightGrey;width:160px;" /> <span id="reqPassWord" controltovalidate="PassWord" errormessage="Password is required !" evaluationfunction="RequiredFieldValidatorEvaluateIsValid" initialvalue="" style="color:Red;visibility:hidden;">Password is required !</span></TD>
</TR>
<tr>
<td>Persistent Cookie</td>
<td><input id="Persist" type="checkbox" name="Persist" tabindex="3" /></td>
</tr>
</TABLE>
</P>
<P><span id="lblFout" style="color:Red;font-size:Medium;">Wrong credentials. Please try again.</span></P>
<P><input type="submit" name="btnLogOn" value="Log On" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="btnLogOn" tabindex="4" /></P>
<script language="javascript" type="text/javascript">
<!--
var Page_Validators = new Array(document.all["reqUserName"], document.all["reqPassWord"]);
// -->
</script>
<script language="javascript"> var control = document.getElementById("UserName"); if( control != null ){control.select();}</script>
<script language="javascript" type="text/javascript">
<!--
var Page_ValidationActive = false;
if (typeof(clientInformation) != "undefined" && clientInformation.appName.indexOf("Explorer") != -1) {
if (typeof(Page_ValidationVer) == "undefined")
alert("Kan de scriptbibliotheek /aspnet_client/system_web/1_1_4322/WebUIValidation.js niet vinden. Probeer het bestand handmatig te plaatsen of installeer het opnieuw door aspnet_regiis -c uit te voeren.");
else if (Page_ValidationVer != "125")
alert("Deze pagina maakt gebruik van een onjuiste versie van WebUIValidation.js. Voor de pagina wordt versie 125 verwacht. De scriptbibliotheek is " + Page_ValidationVer + ".");
else
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
return true;
}
// -->
</script>
</form>
</body>
</HTML>
1) Change Control.ID to Control.ClientID (this isn't your problem, but you should get in the habit of this when mixing client-server code. A control's ID will remain the same serverside, but may change drastically client-side - normally if the control is hosted within another control that run's server side)
2) In your javascript code that finds the controls and sets the focus:
Use control.focus(), followed by control.select
3) tabIndex is an IE-specific property, won't work in other browsers...
And of course, your script may run before the body element is fully loaded. You should also implement an 'onreadystatechange' attribute to your body element.
<head>
<script language="javascript">
function inspectState()
{
if (document.readyState=="complete")
{
// now try focusing your textbox
}
}
</script>
</head>
<body onreadystatechange="inspectState()">
...
</body>
Thank you, works fine now !
you may want to use .ClientID in your codeBehind that write the js call
I tried the following code but the text in the text box is still not highlighted. Any ideas?
Thanks
Code:RegisterStartupScript("ClientSideScript", "<script language=javascript> document.all('TextBox1').focus(); document.all('TextBox1').select; </script>")
try ('" + TextBox1.ClientID + "') depending on the situation you <input type="text"> that is generated by your text box may or may not have an id of TextBox1. If your are using RegistierStartupScript your textbox will be loaded when your script fires so you should be ok on that part.Quote:
Originally Posted by indydavid32
ANother option you may want to consider if you know you are ie only is to use smart navigation. smart navigation track the page and resores it to it's previous state after the post back. I don't know what it does for text boxes and selections.
Also in general you should try to avoid doing a postback when the text on a textbox is changed it's a lot of overhead.
If you really need to validate that text as it is entered in a text box try to use a javascript validation or processing script. If js just won't cut it use a event you raise yourself from the javascript onblur event....
Magiaus, thanks for you help. I couldn't get what you suggested to work. I'm not trying to put the code in postback. As a matter of fact I have the code under If Not ispostback = True. All I'm trying to do is have a particular textbox have the focus when the page loads and the text in the textbox to be highlighted. I got the first part to work, but I can't get the textbox text highlighted.
If you have the first part working you need to look into using the text range features that javascript provides for text boxes. I haven't used them that much or I would try to offer a little more help...
http://www.webreference.com/js/column12/textrange.html
http://www.webreference.com/js/colum...ssbrowser.html
http://www.webreference.com/js/tips/011024.html
http://www.webreference.com/js/column12/
maybe that will help