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

select text in TextBox from code?

P: n/a
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
Aug 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <Da*****@discussions.microsoft.comescreveu na mensagem
news:CD**********************************@microsof t.com...
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

Aug 18 '06 #2

P: n/a
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" <Da*****@discussions.microsoft.comescreveu na mensagem
news:CD**********************************@microsof t.com...
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


Aug 18 '06 #3

P: n/a
for that you use javascript

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

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

"Dabbler" <Da*****@discussions.microsoft.comescreveu na mensagem
news:75**********************************@microsof t.com...
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" <Da*****@discussions.microsoft.comescreveu na mensagem
news:CD**********************************@microso ft.com...
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



Aug 18 '06 #4

P: n/a
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" <Da*****@discussions.microsoft.comescreveu na mensagem
news:75**********************************@microsof t.com...
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" <Da*****@discussions.microsoft.comescreveu na mensagem
news:CD**********************************@microsof t.com...
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


Aug 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.