Results 1 to 19 of 19

Thread: Focus

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Pilipinas
    Posts
    441

    Focus

    Why TextBox1.Focus doesnt work in ASP .NET?? what code should I write on this?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Focus

    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.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: Focus

    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

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Focus

    This code works for my dropdownlists, but not my textboxes. Any ideas?

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Focus

    FYI:
    VB Code:
    1. Private Sub Set_Focus(ByVal oControl As Control)
    2.         Dim strScript As String
    3.         strScript = "<script language=javascript> document.all('" & oControl.ID & "').focus() </script>"
    4.         RegisterStartupScript("ClientSideScript", strScript)
    5.     End Sub

  6. #6
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    Re: Focus

    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:
    1. 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 ?

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Focus

    Have you set the tabindex property for the textbox?

  8. #8
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    Re: Focus

    No, all tabindexes are set to 0.

  9. #9
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Focus

    Change them so your lowest index is 1.

  10. #10
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Focus

    Out of interest, "document.all" is IE specific; it will not work in other browsers. Rather use document.getElementById instead.

  11. #11
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    Re: Focus

    Sorry, but this doesn't help, even with indexes and "document.getElementById ".

    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;" />&nbsp;<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;" />&nbsp;<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>

  12. #12
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: Focus

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

  13. #13
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: Focus

    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>

  14. #14
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    Re: Focus

    Thank you, works fine now !

  15. #15
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Focus

    you may want to use .ClientID in your codeBehind that write the js call
    Magiaus

    If I helped give me some points.

  16. #16
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Re: Focus

    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>")
    David Wilhelm

  17. #17
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Focus

    Quote Originally Posted by indydavid32
    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.

    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

    If I helped give me some points.

  18. #18
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Re: Focus

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

  19. #19
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Focus

    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
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width