select text in TextBox from code?

Discussion in 'ASP .Net' started by =?Utf-8?B?RGFiYmxlcg==?=, Aug 18, 2006.

  1. 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
    =?Utf-8?B?RGFiYmxlcg==?=, Aug 18, 2006
    #1
    1. Advertising

  2. 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" <> escreveu na mensagem
    news:...
    > 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
    Bruno Alexandre, Aug 18, 2006
    #2
    1. Advertising

  3. 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" <> escreveu na mensagem
    > news:...
    > > 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

    >
    >
    >
    =?Utf-8?B?RGFiYmxlcg==?=, Aug 18, 2006
    #3
  4. for that you use javascript

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


    --

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



    "Dabbler" <> escreveu na mensagem
    news:...
    > 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" <> escreveu na mensagem
    >> news:...
    >> > 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

    >>
    >>
    >>
    Bruno Alexandre, Aug 18, 2006
    #4
  5. 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" <> escreveu na mensagem
    > news:...
    > > 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" <> escreveu na mensagem
    > >> news:...
    > >> > 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
    > >>
    > >>
    > >>

    >
    >
    >
    =?Utf-8?B?RGFiYmxlcg==?=, Aug 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. guy
    Replies:
    1
    Views:
    7,034
    John Timney \(Microsoft MVP\)
    Oct 6, 2004
  2. Replies:
    3
    Views:
    738
    George Ter-Saakov
    Feb 23, 2007
  3. agb

    writing two textbox's text in one textbox

    agb, Aug 26, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    163
    Ken Cox [Microsoft MVP]
    Aug 27, 2003
  4. ErwinP
    Replies:
    1
    Views:
    725
    ErwinP
    Aug 19, 2005
  5. palmiere
    Replies:
    1
    Views:
    388
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page