By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Removing default textbox data on click

P: n/a
Hi,

I have a textbox with the words "Enter your name here" inserted as default
text - At the moment, to remove this the user must highlight all the text
and delete before they type in their name - I've seen sites where all this
text dissapears as soon as the user clicks on it - how do I do that ?

Thanks

Jane
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
you can use the GotFocus event of the textbox
"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@hercules.btinternet.com...
Hi,

I have a textbox with the words "Enter your name here" inserted as default
text - At the moment, to remove this the user must highlight all the text
and delete before they type in their name - I've seen sites where all this
text dissapears as soon as the user clicks on it - how do I do that ?

Thanks

Jane

Nov 20 '05 #2

P: n/a
While I probably wouldn't normally hardcode a string literal, this should
give you the idea to get started.

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

Or, to keep the default text there until they start typing:
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.SelectAll()
End If
End Sub
"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@hercules.btinternet.com...
Hi,

I have a textbox with the words "Enter your name here" inserted as default
text - At the moment, to remove this the user must highlight all the text
and delete before they type in their name - I've seen sites where all this
text dissapears as soon as the user clicks on it - how do I do that ?

Thanks

Jane

Nov 20 '05 #3

P: n/a
Thanks, I was going to ask for a pointer..... now I'm going to ask for
another !

I get this error...

Handles clause requires a WithEvents variable

Jane

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
While I probably wouldn't normally hardcode a string literal, this should
give you the idea to get started.

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

Or, to keep the default text there until they start typing:
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.SelectAll()
End If
End Sub
"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@hercules.btinternet.com...
Hi,

I have a textbox with the words "Enter your name here" inserted as default text - At the moment, to remove this the user must highlight all the text and delete before they type in their name - I've seen sites where all this text dissapears as soon as the user clicks on it - how do I do that ?

Thanks

Jane


Nov 20 '05 #4

P: n/a
To create the "GotFocus" handler automatically, do this: drag your textbox
to the form, and give it a name (in the designer). The default name will be
TextBox1 for the first text box.

Go to the code view, and in the left-hand dropdown just above the code,
select "TextBox1", or whatever you named it. Then in the righ-hand
dropdown, select the "GotFocus" item (it will have a little lightning bolt
next to it). This will create the Shell of the event handler.

In the code I sent, it assumes that the text box was named TextBox1. If it
wasnt, change the part that says
.... "Handles TextBox1.GotFocus "
To "Handles TheRealNameOfMyTextBox.GotFocus".

By named, I mean this : in the designer of your form, select the text box.
Look in the properties window and find the "(name)" property. Whatever is
entered there is the "name" of the text box that I keep referring to.


"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...
Thanks, I was going to ask for a pointer..... now I'm going to ask for
another !

I get this error...

Handles clause requires a WithEvents variable

Jane

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
While I probably wouldn't normally hardcode a string literal, this should
give you the idea to get started.

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

Or, to keep the default text there until they start typing:
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.SelectAll()
End If
End Sub
"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@hercules.btinternet.com...
Hi,

I have a textbox with the words "Enter your name here" inserted as

default text - At the moment, to remove this the user must highlight all the text and delete before they type in their name - I've seen sites where all this text dissapears as soon as the user clicks on it - how do I do that ?

Thanks

Jane



Nov 20 '05 #5

P: n/a
And there probably lies my problem, I'm not developing in studio, just
writing code !

"Philip Rieck" <st***@mckraken.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
To create the "GotFocus" handler automatically, do this: drag your textbox
to the form, and give it a name (in the designer). The default name will be TextBox1 for the first text box.

Go to the code view, and in the left-hand dropdown just above the code,
select "TextBox1", or whatever you named it. Then in the righ-hand
dropdown, select the "GotFocus" item (it will have a little lightning bolt
next to it). This will create the Shell of the event handler.

In the code I sent, it assumes that the text box was named TextBox1. If it wasnt, change the part that says
... "Handles TextBox1.GotFocus "
To "Handles TheRealNameOfMyTextBox.GotFocus".

By named, I mean this : in the designer of your form, select the text box.
Look in the properties window and find the "(name)" property. Whatever is
entered there is the "name" of the text box that I keep referring to.


"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...
Thanks, I was going to ask for a pointer..... now I'm going to ask for
another !

I get this error...

Handles clause requires a WithEvents variable

Jane

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
While I probably wouldn't normally hardcode a string literal, this should give you the idea to get started.

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

Or, to keep the default text there until they start typing:
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.SelectAll()
End If
End Sub
"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@hercules.btinternet.com...
> Hi,
>
> I have a textbox with the words "Enter your name here" inserted as

default
> text - At the moment, to remove this the user must highlight all the

text
> and delete before they type in their name - I've seen sites where all
this
> text dissapears as soon as the user clicks on it - how do I do that

? >
> Thanks
>
> Jane
>
>



Nov 20 '05 #6

P: n/a
when defining your textbox add withevents keyword:
private WithEvents myTextbox As TextBox
other solution:

private myTextbox As TextBox
Public Sub New()
addHandler myTextbox.GotFocus, AddressOf TextBox1_GotFocus
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
....
End Sub
or something that looks alike ;-)

"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
And there probably lies my problem, I'm not developing in studio, just
writing code !

"Philip Rieck" <st***@mckraken.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
To create the "GotFocus" handler automatically, do this: drag your textbox
to the form, and give it a name (in the designer). The default name will
be
TextBox1 for the first text box.

Go to the code view, and in the left-hand dropdown just above the code,
select "TextBox1", or whatever you named it. Then in the righ-hand
dropdown, select the "GotFocus" item (it will have a little lightning
bolt next to it). This will create the Shell of the event handler.

In the code I sent, it assumes that the text box was named TextBox1. If

it
wasnt, change the part that says
... "Handles TextBox1.GotFocus "
To "Handles TheRealNameOfMyTextBox.GotFocus".

By named, I mean this : in the designer of your form, select the text box. Look in the properties window and find the "(name)" property. Whatever is entered there is the "name" of the text box that I keep referring to.


"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...
Thanks, I was going to ask for a pointer..... now I'm going to ask for
another !

I get this error...

Handles clause requires a WithEvents variable

Jane

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
> While I probably wouldn't normally hardcode a string literal, this

should
> give you the idea to get started.
>
> Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles TextBox1.GotFocus
> If TextBox1.Text = "Enter your name here" Then
> TextBox1.Text = ""
> End If
> End Sub
>
> Or, to keep the default text there until they start typing:
> Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles TextBox1.GotFocus
> If TextBox1.Text = "Enter your name here" Then
> TextBox1.SelectAll()
> End If
> End Sub
>
>
> "Jane Sharpe" <JS**@hotmail.com> wrote in message
> news:bu**********@hercules.btinternet.com...
> > Hi,
> >
> > I have a textbox with the words "Enter your name here" inserted as
default
> > text - At the moment, to remove this the user must highlight all the text
> > and delete before they type in their name - I've seen sites where

all this
> > text dissapears as soon as the user clicks on it - how do I do
that ? > >
> > Thanks
> >
> > Jane
> >
> >
>
>



Nov 20 '05 #7

P: n/a
Thanks for all this help, but I just CANT get this all to gel.

With this test code, I get the error

BC30590: Event 'GotFocus' cannot be found.
Heres my test code:-

<%@ Page Language="VB" %>

<script runat="server">

Private WithEvents TextBox1 As TextBox
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

</script>

<html>

<body>
<form Id="frmForm1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" BorderStyle="Solid"
Enter your name here</asp:TextBox> </p>
</form>
</body>
</html>

"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:O2**************@TK2MSFTNGP10.phx.gbl... when defining your textbox add withevents keyword:
private WithEvents myTextbox As TextBox
other solution:

private myTextbox As TextBox
Public Sub New()
addHandler myTextbox.GotFocus, AddressOf TextBox1_GotFocus
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
...
End Sub
or something that looks alike ;-)

"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
And there probably lies my problem, I'm not developing in studio, just
writing code !

"Philip Rieck" <st***@mckraken.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
To create the "GotFocus" handler automatically, do this: drag your textbox to the form, and give it a name (in the designer). The default name will
be
TextBox1 for the first text box.

Go to the code view, and in the left-hand dropdown just above the code, select "TextBox1", or whatever you named it. Then in the righ-hand
dropdown, select the "GotFocus" item (it will have a little lightning bolt next to it). This will create the Shell of the event handler.

In the code I sent, it assumes that the text box was named TextBox1. If it
wasnt, change the part that says
... "Handles TextBox1.GotFocus "
To "Handles TheRealNameOfMyTextBox.GotFocus".

By named, I mean this : in the designer of your form, select the text box. Look in the properties window and find the "(name)" property.
Whatever
is entered there is the "name" of the text box that I keep referring to.


"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...
> Thanks, I was going to ask for a pointer..... now I'm going to ask
for > another !
>
> I get this error...
>
> Handles clause requires a WithEvents variable
>
> Jane
>
> "Philip Rieck" <st***@mckraken.com> wrote in message
> news:eH**************@tk2msftngp13.phx.gbl...
> > While I probably wouldn't normally hardcode a string literal, this
should
> > give you the idea to get started.
> >
> > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles TextBox1.GotFocus
> > If TextBox1.Text = "Enter your name here" Then
> > TextBox1.Text = ""
> > End If
> > End Sub
> >
> > Or, to keep the default text there until they start typing:
> > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles TextBox1.GotFocus
> > If TextBox1.Text = "Enter your name here" Then
> > TextBox1.SelectAll()
> > End If
> > End Sub
> >
> >
> > "Jane Sharpe" <JS**@hotmail.com> wrote in message
> > news:bu**********@hercules.btinternet.com...
> > > Hi,
> > >
> > > I have a textbox with the words "Enter your name here" inserted as > default
> > > text - At the moment, to remove this the user must highlight all the > text
> > > and delete before they type in their name - I've seen sites

where all
> this
> > > text dissapears as soon as the user clicks on it - how do I do

that
?
> > >
> > > Thanks
> > >
> > > Jane
> > >
> > >
> >
> >
>
>



Nov 20 '05 #8

P: n/a
hum
you forgot to specify one little detail
it's a webapplication...

try this group: microsoft.public.dotnet.framework.aspnet


"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
Thanks for all this help, but I just CANT get this all to gel.

With this test code, I get the error

BC30590: Event 'GotFocus' cannot be found.
Heres my test code:-

<%@ Page Language="VB" %>

<script runat="server">

Private WithEvents TextBox1 As TextBox
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

</script>

<html>

<body>
<form Id="frmForm1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" BorderStyle="Solid"
Enter your name here</asp:TextBox> </p>
</form>
</body>
</html>

"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:O2**************@TK2MSFTNGP10.phx.gbl...
when defining your textbox add withevents keyword:
private WithEvents myTextbox As TextBox
other solution:

private myTextbox As TextBox
Public Sub New()
addHandler myTextbox.GotFocus, AddressOf TextBox1_GotFocus
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
...
End Sub
or something that looks alike ;-)

"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
And there probably lies my problem, I'm not developing in studio, just
writing code !

"Philip Rieck" <st***@mckraken.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
> To create the "GotFocus" handler automatically, do this: drag your

textbox
> to the form, and give it a name (in the designer). The default name

will
be
> TextBox1 for the first text box.
>
> Go to the code view, and in the left-hand dropdown just above the code, > select "TextBox1", or whatever you named it. Then in the righ-hand
> dropdown, select the "GotFocus" item (it will have a little lightning
bolt
> next to it). This will create the Shell of the event handler.
>
> In the code I sent, it assumes that the text box was named TextBox1. If it
> wasnt, change the part that says
> ... "Handles TextBox1.GotFocus "
> To "Handles TheRealNameOfMyTextBox.GotFocus".
>
> By named, I mean this : in the designer of your form, select the
text
box.
> Look in the properties window and find the "(name)" property. Whatever
is
> entered there is the "name" of the text box that I keep referring

to. >
>
>
>
> "Jane Sharpe" <JS**@hotmail.com> wrote in message
> news:bu**********@sparta.btinternet.com...
> > Thanks, I was going to ask for a pointer..... now I'm going to ask

for > > another !
> >
> > I get this error...
> >
> > Handles clause requires a WithEvents variable
> >
> > Jane
> >
> > "Philip Rieck" <st***@mckraken.com> wrote in message
> > news:eH**************@tk2msftngp13.phx.gbl...
> > > While I probably wouldn't normally hardcode a string literal, this > should
> > > give you the idea to get started.
> > >
> > > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> > > System.EventArgs) Handles TextBox1.GotFocus
> > > If TextBox1.Text = "Enter your name here" Then
> > > TextBox1.Text = ""
> > > End If
> > > End Sub
> > >
> > > Or, to keep the default text there until they start typing:
> > > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> > > System.EventArgs) Handles TextBox1.GotFocus
> > > If TextBox1.Text = "Enter your name here" Then
> > > TextBox1.SelectAll()
> > > End If
> > > End Sub
> > >
> > >
> > > "Jane Sharpe" <JS**@hotmail.com> wrote in message
> > > news:bu**********@hercules.btinternet.com...
> > > > Hi,
> > > >
> > > > I have a textbox with the words "Enter your name here"
inserted as > > default
> > > > text - At the moment, to remove this the user must highlight
all the
> > text
> > > > and delete before they type in their name - I've seen sites

where all
> > this
> > > > text dissapears as soon as the user clicks on it - how do I do

that
?
> > > >
> > > > Thanks
> > > >
> > > > Jane
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #9

P: n/a
AhHa! it's a web application. That makes it a different beast:
The GotFocus event doesn't really make sense in a web application, since the
events in your VB.NET code are fired on the server side, not the client
side. To do this, you need to use some javascript.

Try this instead:

<%@ Page Language="VB" %>
<HTML>
<script runat="server">

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
TextBox1.Attributes.Add("onFocus", "removeText('" & TextBox1.ClientId &
"');")
End Sub

</script>

<script language=javascript>
function removeText( clientId)
{
document.getElementById( clientId).value = '';
}
</script>
<body>
<form Id="frmForm1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" BorderStyle="Solid">
Enter your name here</asp:TextBox>
</p>
</form>
</body>
</HTML>


"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
Thanks for all this help, but I just CANT get this all to gel.

With this test code, I get the error

BC30590: Event 'GotFocus' cannot be found.
Heres my test code:-

<%@ Page Language="VB" %>

<script runat="server">

Private WithEvents TextBox1 As TextBox
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

</script>

<html>

<body>
<form Id="frmForm1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" BorderStyle="Solid"
Enter your name here</asp:TextBox> </p>
</form>
</body>
</html>

"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:O2**************@TK2MSFTNGP10.phx.gbl...
when defining your textbox add withevents keyword:
private WithEvents myTextbox As TextBox
other solution:

private myTextbox As TextBox
Public Sub New()
addHandler myTextbox.GotFocus, AddressOf TextBox1_GotFocus
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
...
End Sub
or something that looks alike ;-)

"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
And there probably lies my problem, I'm not developing in studio, just
writing code !

"Philip Rieck" <st***@mckraken.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
> To create the "GotFocus" handler automatically, do this: drag your

textbox
> to the form, and give it a name (in the designer). The default name

will
be
> TextBox1 for the first text box.
>
> Go to the code view, and in the left-hand dropdown just above the code, > select "TextBox1", or whatever you named it. Then in the righ-hand
> dropdown, select the "GotFocus" item (it will have a little lightning
bolt
> next to it). This will create the Shell of the event handler.
>
> In the code I sent, it assumes that the text box was named TextBox1. If it
> wasnt, change the part that says
> ... "Handles TextBox1.GotFocus "
> To "Handles TheRealNameOfMyTextBox.GotFocus".
>
> By named, I mean this : in the designer of your form, select the
text
box.
> Look in the properties window and find the "(name)" property. Whatever
is
> entered there is the "name" of the text box that I keep referring

to. >
>
>
>
> "Jane Sharpe" <JS**@hotmail.com> wrote in message
> news:bu**********@sparta.btinternet.com...
> > Thanks, I was going to ask for a pointer..... now I'm going to ask

for > > another !
> >
> > I get this error...
> >
> > Handles clause requires a WithEvents variable
> >
> > Jane
> >
> > "Philip Rieck" <st***@mckraken.com> wrote in message
> > news:eH**************@tk2msftngp13.phx.gbl...
> > > While I probably wouldn't normally hardcode a string literal, this > should
> > > give you the idea to get started.
> > >
> > > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> > > System.EventArgs) Handles TextBox1.GotFocus
> > > If TextBox1.Text = "Enter your name here" Then
> > > TextBox1.Text = ""
> > > End If
> > > End Sub
> > >
> > > Or, to keep the default text there until they start typing:
> > > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
> > > System.EventArgs) Handles TextBox1.GotFocus
> > > If TextBox1.Text = "Enter your name here" Then
> > > TextBox1.SelectAll()
> > > End If
> > > End Sub
> > >
> > >
> > > "Jane Sharpe" <JS**@hotmail.com> wrote in message
> > > news:bu**********@hercules.btinternet.com...
> > > > Hi,
> > > >
> > > > I have a textbox with the words "Enter your name here"
inserted as > > default
> > > > text - At the moment, to remove this the user must highlight
all the
> > text
> > > > and delete before they type in their name - I've seen sites

where all
> > this
> > > > text dissapears as soon as the user clicks on it - how do I do

that
?
> > > >
> > > > Thanks
> > > >
> > > > Jane
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #10

P: n/a
Did I not mention that little detail, my apologies, its confusing enough
finding the right newsgroup !!

Anyway, I'm working now, so Thank You, Thank You, Thank You !!

Jane

"Philip Rieck" <st***@mckraken.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
AhHa! it's a web application. That makes it a different beast:
The GotFocus event doesn't really make sense in a web application, since the events in your VB.NET code are fired on the server side, not the client
side. To do this, you need to use some javascript.

Try this instead:

<%@ Page Language="VB" %>
<HTML>
<script runat="server">

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
TextBox1.Attributes.Add("onFocus", "removeText('" & TextBox1.ClientId &
"');")
End Sub

</script>

<script language=javascript>
function removeText( clientId)
{
document.getElementById( clientId).value = '';
}
</script>
<body>
<form Id="frmForm1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" BorderStyle="Solid">
Enter your name here</asp:TextBox>
</p>
</form>
</body>
</HTML>


"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
Thanks for all this help, but I just CANT get this all to gel.

With this test code, I get the error

BC30590: Event 'GotFocus' cannot be found.
Heres my test code:-

<%@ Page Language="VB" %>

<script runat="server">

Private WithEvents TextBox1 As TextBox
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "Enter your name here" Then
TextBox1.Text = ""
End If
End Sub

</script>

<html>

<body>
<form Id="frmForm1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" BorderStyle="Solid"
Enter your name here</asp:TextBox>

</p>
</form>
</body>
</html>

"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:O2**************@TK2MSFTNGP10.phx.gbl...
when defining your textbox add withevents keyword:
private WithEvents myTextbox As TextBox
other solution:

private myTextbox As TextBox
Public Sub New()
addHandler myTextbox.GotFocus, AddressOf TextBox1_GotFocus
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
...
End Sub
or something that looks alike ;-)

"Jane Sharpe" <JS**@hotmail.com> wrote in message
news:bu**********@titan.btinternet.com...
> And there probably lies my problem, I'm not developing in studio, just > writing code !
>
> "Philip Rieck" <st***@mckraken.com> wrote in message
> news:uj**************@TK2MSFTNGP12.phx.gbl...
> > To create the "GotFocus" handler automatically, do this: drag your
textbox
> > to the form, and give it a name (in the designer). The default name will
> be
> > TextBox1 for the first text box.
> >
> > Go to the code view, and in the left-hand dropdown just above the

code,
> > select "TextBox1", or whatever you named it. Then in the righ-hand > > dropdown, select the "GotFocus" item (it will have a little lightning bolt
> > next to it). This will create the Shell of the event handler.
> >
> > In the code I sent, it assumes that the text box was named TextBox1.
If
> it
> > wasnt, change the part that says
> > ... "Handles TextBox1.GotFocus "
> > To "Handles TheRealNameOfMyTextBox.GotFocus".
> >
> > By named, I mean this : in the designer of your form, select the text box.
> > Look in the properties window and find the "(name)" property.

Whatever
is
> > entered there is the "name" of the text box that I keep referring to. > >
> >
> >
> >
> > "Jane Sharpe" <JS**@hotmail.com> wrote in message
> > news:bu**********@sparta.btinternet.com...
> > > Thanks, I was going to ask for a pointer..... now I'm going to
ask
for
> > > another !
> > >
> > > I get this error...
> > >
> > > Handles clause requires a WithEvents variable
> > >
> > > Jane
> > >
> > > "Philip Rieck" <st***@mckraken.com> wrote in message
> > > news:eH**************@tk2msftngp13.phx.gbl...
> > > > While I probably wouldn't normally hardcode a string literal,

this > > should
> > > > give you the idea to get started.
> > > >
> > > > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e
As > > > > System.EventArgs) Handles TextBox1.GotFocus
> > > > If TextBox1.Text = "Enter your name here" Then
> > > > TextBox1.Text = ""
> > > > End If
> > > > End Sub
> > > >
> > > > Or, to keep the default text there until they start typing:
> > > > Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As > > > > System.EventArgs) Handles TextBox1.GotFocus
> > > > If TextBox1.Text = "Enter your name here" Then
> > > > TextBox1.SelectAll()
> > > > End If
> > > > End Sub
> > > >
> > > >
> > > > "Jane Sharpe" <JS**@hotmail.com> wrote in message
> > > > news:bu**********@hercules.btinternet.com...
> > > > > Hi,
> > > > >
> > > > > I have a textbox with the words "Enter your name here"

inserted
as
> > > default
> > > > > text - At the moment, to remove this the user must highlight

all the
> > > text
> > > > > and delete before they type in their name - I've seen sites

where
> all
> > > this
> > > > > text dissapears as soon as the user clicks on it - how do I do that
> ?
> > > > >
> > > > > Thanks
> > > > >
> > > > > Jane
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.