Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 21st, 2004, 01:12 PM
Authorized User
 
Join Date: Nov 2004
Location: , , .
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
  #2 (permalink)  
Old November 22nd, 2004, 12:31 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,346
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.

  #3 (permalink)  
Old November 22nd, 2004, 01:52 PM
Authorized User
 
Join Date: Nov 2004
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

thanks a lot.

yengzhai
  #4 (permalink)  
Old November 22nd, 2004, 02:05 PM
Authorized User
 
Join Date: Nov 2004
Location: , , .
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
  #5 (permalink)  
Old November 22nd, 2004, 02:34 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,346
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.

  #6 (permalink)  
Old November 23rd, 2004, 12:09 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
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
  #7 (permalink)  
Old November 23rd, 2004, 01:22 PM
Authorized User
 
Join Date: Nov 2004
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

yengzhai
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 01:54 PM
How to check if a textbox has focus using c#? Hannibal ASP.NET 2.0 Basics 0 May 15th, 2007 02: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 07:45 PM



All times are GMT -4. The time now is 05:58 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.