473,379 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

TextBox Focus Question

Hi,

I have more experience working with Windows.Forms than with WebForms
and sometimes it trips me up.

When my page loads, I want a particular textbox to have the focus. I
am attempting to do it using logic like this:

if TextBox1 CanFocus then
TextBox1 Focus()
end if

But the compiler does not like that because the web version of the
TextBox class does not have CanFocus or Focus()

How do I set the focus to the desired textbox?
JD
Nov 19 '05 #1
3 1704
Since focus is a client side action you'll have to do it with some
JavaScript. In version 1.1 of the framework, you can follow the
instructions on http://www.davefancher.com/BlogEntry.aspx?ArticleID=14 [C#
example but the conversion should be easy]. Version 2 has this
functionality built in.

HTH
----------------
Dave Fancher
http://www.davefancher.com

"Joe Delphi" <delphi561@no****@cox.net> wrote in message
news:kDEwe.3704$Qo.3702@fed1read01...
Hi,

I have more experience working with Windows.Forms than with WebForms
and sometimes it trips me up.

When my page loads, I want a particular textbox to have the focus.
I
am attempting to do it using logic like this:

if TextBox1 CanFocus then
TextBox1 Focus()
end if

But the compiler does not like that because the web version of the
TextBox class does not have CanFocus or Focus()

How do I set the focus to the desired textbox?
JD

Nov 19 '05 #2
Joe you can use this in page_load
and make sure you import "Imports System.Text
and txtAge is the ID of your textbox

Dim strBuilder As StringBuilder = New StringBuilder
strBuilder.Append("<script language='javascript'>")
strBuilder.Append("document.getElementById('txtAge ').focus()")
strBuilder.Append("</script>")
RegisterStartupScript("Focus", strBuilder.ToString)

Hope it helps
Patrick
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #3
Hi Joe,

First of all you should understand that web application is different from
desktop application. The web application runs on a client-server mode. Any
web server control only exists on server-side. Once rendering to client-side,
it is destroyed. And corresponding html component is shown on client-side.
Actually a 'focus' event occurs on client-side. So client-side JavaScript
code can be used to conduct focus and other events.

Following code snippet shows how to set TextBox focus:

String strFocus = "<script>document.all." + txtbox.ID + ".focus();</script>";
Page.RegisterStartupScript("focusTextBox", strFocus);

HTH

Elton Wang
el********@hotmail.com

"Joe Delphi" wrote:
Hi,

I have more experience working with Windows.Forms than with WebForms
and sometimes it trips me up.

When my page loads, I want a particular textbox to have the focus. I
am attempting to do it using logic like this:

if TextBox1 CanFocus then
TextBox1 Focus()
end if

But the compiler does not like that because the web version of the
TextBox class does not have CanFocus or Focus()

How do I set the focus to the desired textbox?
JD

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: M O J O | last post by:
Hi, Maybe this question has been asked before, but I've searched the net without finding any answer. I create a new solution, put a TabControl on Form1, create a TabPage and a put a TextBox...
4
by: Mad Scientist Jr | last post by:
I have a textbox that i am adding to (in codebehind of ASP.NET) and need to ensure that the focus is scrolled to the bottom of the textbox each time the page refreshes, and then set focus to a 2nd...
0
by: Jonas L | last post by:
Hi, I need to create a textbox which acts as a normal textbox but with the following extra requirements: 1) In-focus color, when the textbox gets focus the backcolor property of the textbox...
3
by: | last post by:
Hi , I did ask before but got no replies. I have a series of textboxes, In Enter event I need to set the focus to a different textbox I am having problems when I set focus within the Textbox...
5
by: Tosch | last post by:
I have a usercontrol with a label, a textbox, a treeview, a grid and a couple of checkboxes. The usercontrol is hosted on a form together with a cancel and a accept button. This form is used to...
2
by: Sridhar | last post by:
Hi, I have a question. I have created a user control which contains a textbox and a button. we will enter some search word in the textbox and hit the button. Then we will get a pop-up with the...
8
by: cj | last post by:
I asked this question a couple of days ago but am just now looking at it again. I used to use the textbox gotfoucs event to have all the text in the textbox selected when it gotfocus. That...
5
by: sherifffruitfly | last post by:
Hi, I want to derive of of TextBox, and add on an auto-scroll-to-bottom boolean property. I can use a regular textbox (rich or otherwise) and get the effect by: mytextbox.append("my text");...
10
daoxx
by: daoxx | last post by:
Hello I've searched, posted and solved this (Thanks for helping!), but now it came back to bite me in the *ss. I have a Yes/No field that is represented by a check box in my form1. I want to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.