473,804 Members | 3,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1729
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@fed1re ad01...
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.Appe nd("<script language='javas cript'>")
strBuilder.Appe nd("document.ge tElementById('t xtAge').focus() ")
strBuilder.Appe nd("</script>")
RegisterStartup Script("Focus", strBuilder.ToSt ring)

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>docume nt.all." + txtbox.ID + ".focus();</script>";
Page.RegisterSt artupScript("fo cusTextBox", strFocus);

HTH

Elton Wang
el********@hotm ail.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
420
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 in this TabPage. I run the project and the TextBox does not have focus. :o(
4
7226
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 textbox (which the user types input into). I have tried a couple functions I found posted online but they don't work (see below) The closest I got was using the scrolldown method:
0
2134
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 should be set to a, selectable, in-focus color. 2) Out-of-focus color, when the textbox loses focus the backcolor property of the textbox should be set to a, selectable, out-of-focus color.
3
1556
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 Enter event. It is looping thru the events. Does anyone know how I can set focus to another object when the Textbox has received focus either by keyboard or mouse event.
5
8731
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 search records in a database. On several forms in my application I show (modal) this form to search records. Everytime I show the form I want the focus to be on the textbox on the usercontrol. This works the first time I display the form. But...
2
1919
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 listbox that contains the matches. Once I select one of the matches, it will close the pop-up and display the selected one in a label control which is also in the user control. Everything is working fine. But I am not getting one thing. After the...
8
21904
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 doesn't seem to work in .net when the textbox receives focus via a mouse click. Jeffrey and Shane both advised how to get a mouse click to select all the text (thank you both) but using the mousedown or mouseup events doesn't work the way I want it...
5
2664
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"); mytextbox.focus(); mytextbox.scrolltocaret();
10
10489
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 show another field (textbox) when the mouse pointer goes over the checkbox, and make it not visible when the mouse moves away from it. My problem is that when I make the textbox diseappear, if the user set focus on it and moves the mouse away, Access...
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.