Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > select text in TextBox from code?

Reply
Thread Tools

select text in TextBox from code?

 
 
=?Utf-8?B?RGFiYmxlcg==?=
Guest
Posts: n/a
 
      08-18-2006
Is there a way to mark the text in a TextBox control as selected so when the
user types a new value the existing text is replaced?

Thanks
 
Reply With Quote
 
 
 
 
Bruno Alexandre
Guest
Posts: n/a
 
      08-18-2006
just using javascript and add the event OnFocus on the textbox

you can implement in many ways:

when the user focus the textbox you delete all the value and present it as
new textbox
or when the user focus the textbox you delete all and if the user did not
changed the value you paste the old value back in the textbox


imagine that you have 2 textboxs called TextBox1 and TextBox2

onPageLoad add the onfous and onblur event to the textbox like:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
TextBox1.Attributes.Add("onfocus", "saveValue(this);")
TextBox1.Attributes.Add("onblur", "getValue(this);")
TextBox2.Attributes.Add("onfocus", "saveValue(this);")
TextBox2.Attributes.Add("onblur", "getValue(this);")
' for testing propose let's add some text on it
TextBox1.Text = "old value 1"
TextBox2.Text = "old value 2"
End Sub


and add the really simple javascript function:
<script language="javascript" type="text/javascript">
var oldValue = ''; // it will save our old values

function getValue( v ) {
if(v.value == '')
v.value = oldValue;
}
function saveValue( v ) {
oldValue = v.value;
v.value = '';
}
</script>


hope it helps

--

Bruno Alexandre
"a Portuguese in KÝbenhav, Danmark"



"Dabbler" <(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> Is there a way to mark the text in a TextBox control as selected so when
> the
> user types a new value the existing text is replaced?
>
> Thanks



 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGFiYmxlcg==?=
Guest
Posts: n/a
 
      08-18-2006
Hi Bruno

Well, I'm setting focus from code behind already. Would just like to
highlight the existing text and let user choose to leave it or delete it by
typing a new value. Is it possible to trigger Ctrl-A from javascript which
would select everything in the TextBox?

Thanks.

"Bruno Alexandre" wrote:

> just using javascript and add the event OnFocus on the textbox
>
> you can implement in many ways:
>
> when the user focus the textbox you delete all the value and present it as
> new textbox
> or when the user focus the textbox you delete all and if the user did not
> changed the value you paste the old value back in the textbox
>
>
> imagine that you have 2 textboxs called TextBox1 and TextBox2
>
> onPageLoad add the onfous and onblur event to the textbox like:
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
> TextBox1.Attributes.Add("onfocus", "saveValue(this);")
> TextBox1.Attributes.Add("onblur", "getValue(this);")
> TextBox2.Attributes.Add("onfocus", "saveValue(this);")
> TextBox2.Attributes.Add("onblur", "getValue(this);")
> ' for testing propose let's add some text on it
> TextBox1.Text = "old value 1"
> TextBox2.Text = "old value 2"
> End Sub
>
>
> and add the really simple javascript function:
> <script language="javascript" type="text/javascript">
> var oldValue = ''; // it will save our old values
>
> function getValue( v ) {
> if(v.value == '')
> v.value = oldValue;
> }
> function saveValue( v ) {
> oldValue = v.value;
> v.value = '';
> }
> </script>
>
>
> hope it helps
>
> --
>
> Bruno Alexandre
> "a Portuguese in K√łbenhav, Danmark"
>
>
>
> "Dabbler" <(E-Mail Removed)> escreveu na mensagem
> news:(E-Mail Removed)...
> > Is there a way to mark the text in a TextBox control as selected so when
> > the
> > user types a new value the existing text is replaced?
> >
> > Thanks

>
>
>

 
Reply With Quote
 
Bruno Alexandre
Guest
Posts: n/a
 
      08-18-2006
for that you use javascript

document.getIdFromElement("Textbox1").select();


--

Bruno Alexandre
"a Portuguese in KÝbenhav, Danmark"



"Dabbler" <(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> Hi Bruno
>
> Well, I'm setting focus from code behind already. Would just like to
> highlight the existing text and let user choose to leave it or delete it
> by
> typing a new value. Is it possible to trigger Ctrl-A from javascript which
> would select everything in the TextBox?
>
> Thanks.
>
> "Bruno Alexandre" wrote:
>
>> just using javascript and add the event OnFocus on the textbox
>>
>> you can implement in many ways:
>>
>> when the user focus the textbox you delete all the value and present it
>> as
>> new textbox
>> or when the user focus the textbox you delete all and if the user did not
>> changed the value you paste the old value back in the textbox
>>
>>
>> imagine that you have 2 textboxs called TextBox1 and TextBox2
>>
>> onPageLoad add the onfous and onblur event to the textbox like:
>> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
>> System.EventArgs)
>> TextBox1.Attributes.Add("onfocus", "saveValue(this);")
>> TextBox1.Attributes.Add("onblur", "getValue(this);")
>> TextBox2.Attributes.Add("onfocus", "saveValue(this);")
>> TextBox2.Attributes.Add("onblur", "getValue(this);")
>> ' for testing propose let's add some text on it
>> TextBox1.Text = "old value 1"
>> TextBox2.Text = "old value 2"
>> End Sub
>>
>>
>> and add the really simple javascript function:
>> <script language="javascript" type="text/javascript">
>> var oldValue = ''; // it will save our old values
>>
>> function getValue( v ) {
>> if(v.value == '')
>> v.value = oldValue;
>> }
>> function saveValue( v ) {
>> oldValue = v.value;
>> v.value = '';
>> }
>> </script>
>>
>>
>> hope it helps
>>
>> --
>>
>> Bruno Alexandre
>> "a Portuguese in KÝbenhav, Danmark"
>>
>>
>>
>> "Dabbler" <(E-Mail Removed)> escreveu na mensagem
>> news:(E-Mail Removed)...
>> > Is there a way to mark the text in a TextBox control as selected so
>> > when
>> > the
>> > user types a new value the existing text is replaced?
>> >
>> > Thanks

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?RGFiYmxlcg==?=
Guest
Posts: n/a
 
      08-18-2006
Thanks for the tip Bruno,

sorry, I'm still new at js from ASP.NET, how would I call this from
codebehind in pageload?

Thanks.




"Bruno Alexandre" wrote:

> for that you use javascript
>
> document.getIdFromElement("Textbox1").select();
>
>
> --
>
> Bruno Alexandre
> "a Portuguese in K√łbenhav, Danmark"
>
>
>
> "Dabbler" <(E-Mail Removed)> escreveu na mensagem
> news:(E-Mail Removed)...
> > Hi Bruno
> >
> > Well, I'm setting focus from code behind already. Would just like to
> > highlight the existing text and let user choose to leave it or delete it
> > by
> > typing a new value. Is it possible to trigger Ctrl-A from javascript which
> > would select everything in the TextBox?
> >
> > Thanks.
> >
> > "Bruno Alexandre" wrote:
> >
> >> just using javascript and add the event OnFocus on the textbox
> >>
> >> you can implement in many ways:
> >>
> >> when the user focus the textbox you delete all the value and present it
> >> as
> >> new textbox
> >> or when the user focus the textbox you delete all and if the user did not
> >> changed the value you paste the old value back in the textbox
> >>
> >>
> >> imagine that you have 2 textboxs called TextBox1 and TextBox2
> >>
> >> onPageLoad add the onfous and onblur event to the textbox like:
> >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> >> System.EventArgs)
> >> TextBox1.Attributes.Add("onfocus", "saveValue(this);")
> >> TextBox1.Attributes.Add("onblur", "getValue(this);")
> >> TextBox2.Attributes.Add("onfocus", "saveValue(this);")
> >> TextBox2.Attributes.Add("onblur", "getValue(this);")
> >> ' for testing propose let's add some text on it
> >> TextBox1.Text = "old value 1"
> >> TextBox2.Text = "old value 2"
> >> End Sub
> >>
> >>
> >> and add the really simple javascript function:
> >> <script language="javascript" type="text/javascript">
> >> var oldValue = ''; // it will save our old values
> >>
> >> function getValue( v ) {
> >> if(v.value == '')
> >> v.value = oldValue;
> >> }
> >> function saveValue( v ) {
> >> oldValue = v.value;
> >> v.value = '';
> >> }
> >> </script>
> >>
> >>
> >> hope it helps
> >>
> >> --
> >>
> >> Bruno Alexandre
> >> "a Portuguese in K√łbenhav, Danmark"
> >>
> >>
> >>
> >> "Dabbler" <(E-Mail Removed)> escreveu na mensagem
> >> news:(E-Mail Removed)...
> >> > Is there a way to mark the text in a TextBox control as selected so
> >> > when
> >> > the
> >> > user types a new value the existing text is replaced?
> >> >
> >> > Thanks
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Textbox, Input (text) on turbotax... anyone know which 3rd party textbox this is??? pld888@gmail.com ASP .Net 3 02-23-2007 08:51 PM
ASP.NET textbox loses text value in composite control but Textbox subclass doesn't!? ErwinP ASP .Net Web Controls 1 08-19-2005 01:50 PM
How to select all text in textbox when it gets focus? guy ASP .Net 1 10-06-2004 09:40 PM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM
writing two textbox's text in one textbox agb ASP .Net Web Controls 1 08-27-2003 12:57 AM



Advertisments