Easy: clicking in a textbox

eramgarden

Contributor
Joined
Mar 8, 2004
Messages
579
I have a textbox, when users click in it,i want to display a text.

Now, textbox doesnt have "onClick" event. onInit works but that would put the text when the page is loaded. I want the text to appear when users click on the textbox..

any ideas?
 
I would do it client-side with javascript using the client-side onclick event for the "textbox" (it's actually rendered as an HTML input of type text).

There are a few ways you could accomplish this so that when your page is first loaded the text that appears is provided (ahead of time) by your code behind.

You can use the Attributes collection of your textbox object in codebehind to create the javascript event code:

Code:
txtYourTextBox.Attributes.Add("onclick", "this.value='your text here'")

Something like this lets you predetermine what text will show but it won't show until the click occurs client-side and no postback is required to make the text show. For example, in the snippet above instead of 'your text here' you might want to provide text based on a variable, database, etc.

Or you might use a hidden input to store the text you're going to show and hardcode the javascript to fill the textbox using the hidden input's value.

Paul
 
Thanks, now i have another issue:

I did this:
Code:
'Dim test = System.DateTime.Now
        'txtAddNotes.Attributes.Add("onclick", "this.value= '" & test & " " & Session("name") &  " '")

I click on the text box, datetime and username appears. so far so good

BUT the cursor is on the beginning of the textbox not at the end of the added text!! I click Enter but the text also moves.

How can I have the cursor at the end of the added text??
 
Back
Top