Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2004, 01:12 PM
Authorized User
 
Join Date: Nov 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to set focus in textbox

hi, can any one share me how to have focus set to the textbox.

thanks.

yengzhai
__________________
yengzhai
 
Old November 22nd, 2004, 12:31 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

you have to use the tabindex property to set the tab sequence or use javacript in the HTML to set the focus.

 
Old November 22nd, 2004, 01:52 PM
Authorized User
 
Join Date: Nov 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oh... i see... use only tab index...

thanks a lot.

yengzhai
 
Old November 22nd, 2004, 02:05 PM
Authorized User
 
Join Date: Nov 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i have tried it, it doesn't work.

i don't know javascript much. now i use c# to manipulate aspnet web control, can javascript manipulate aspnet web control as well? may you just show me a very simple example using javascript to manipulate the web control?

yengzhai
 
Old November 22nd, 2004, 02:34 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Here is the page (HTML) I used for the example. There are 2 textboxes on the form. Notice the <BODY> tag.

<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout" onload="javascript:document.Form1.TextBox2.focus() ;">
<form id="Form1" method="post" runat="server">
            <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 96px" runat="server"></asp:TextBox>
            <asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 304px; POSITION: absolute; TOP: 232px"
                runat="server"></asp:TextBox>
        </form>
    </body>
</HTML>

That is how you can use javascript to set focus:
onload="javascript:document.<form name>.<control name>.focus();"
remember, this must be in the <BODY> tag of the HTML.

 
Old November 23rd, 2004, 12:09 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here is a function that I have found very useful for setting focus. I have this function in a helper class so I can call it from anywhere:

Public Shared Sub SetFocus(ByVal page As System.Web.UI.Page, ByVal control As System.Web.UI.Control)
    Dim strScript As String = String.Format("document.getElementById('{0}').focu s();", control.ClientID)
    strScript = String.Format("<script language=""JavaScript"">{0}</script>", strScript)
    page.RegisterStartupScript("setFocus", strScript)
End Sub
 
Old November 23rd, 2004, 01:22 PM
Authorized User
 
Join Date: Nov 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for the solution given. now i have learnt some new thing. :)

yengzhai





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set focus on particular char in textbox anil.goyal ASP.NET 1.0 and 1.1 Basics 1 June 9th, 2008 12:54 PM
How to check if a textbox has focus using c#? Hannibal ASP.NET 2.0 Basics 0 May 15th, 2007 01:02 AM
set focus sameer_1981 Intro Programming 1 February 27th, 2007 09:14 AM
How to get insertion point to focus in textbox? larry Javascript How-To 2 April 30th, 2004 06:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.