Solved

Clear text out when first clicked in Text Box in DataGrid

Posted on 2004-09-27
18
252 Views
Last Modified: 2008-02-01
When the text box is first clicked, I want the default Text to clear.
Problem is that this control must be drawn in code, in a DataGrid:

Private Sub grid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grid.ItemDataBound
                . . . . .
                Dim tx As New TextBox()
                tx.ID = "txtEng"
                tx.TextMode = TextBoxMode.MultiLine
                tx.Text = "Add message here.  Click Add when done."
                e.Item.Cells(3).Controls.Add(tx)

    End Sub

How can add an event handler to a control that is not available until the
code is run?
Thanks
0
Comment
Question by:Bytes55
  • 7
  • 4
  • 2
  • +1
18 Comments
 
LVL 15

Accepted Solution

by:
Thogek earned 43 total points
ID: 12165216
Something to try:

    Dim tx As New TextBox()
    tx.ID = "txtEng"
    tx.TextMode = TextBoxMode.MultiLine
    tx.Text = "Add message here.  Click Add when done."
    tx.Attributes.Add("onfocus", "if(this.value=='" + tx.Text + "') this.value=='';");
    e.Item.Cells(3).Controls.Add(tx)
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12165221
Oops.  Small typo:

    tx.Attributes.Add("onfocus", "if(this.value=='" + tx.Text + "') this.value='';");
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 41 total points
ID: 12165344
Thogek is right, but I think you would not want the ";", just an empty string (?).  Having said that, I would only add this:

tx.Attributes.Add("onblur", "javascript:if (this.value = '') this.value = '" + tx.Text + "';";

This way if they leave the textbox without putting something in, it puts back in your default.

John

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:Thogek
ID: 12165417
Uh... I have a semi-colon (;) *after* the string (delimited by single-quotes), not within it -- just like you do within your onblur code.

I like the idea of adding the complentary onblur, though.  :-)
0
 

Author Comment

by:Bytes55
ID: 12165459
Thogek, jnhorst, thank you

Thogek, your solution worked perfect.
jnhorst, I liked your idea about putting back the default text in the text box
and now I want that, but the code above didn't work.

How can I get the default text back in if the text field is left blank?

0
 
LVL 15

Expert Comment

by:Thogek
ID: 12165489
Try

    tx.Attributes.Add("onblur", "if(this.value=='') this.value='" + tx.Text + "';");
0
 

Author Comment

by:Bytes55
ID: 12165710
No luck with that last code . . it does the same thing as jnhorst's first attempt.

I'm using VB and I changed the "==" to "=" and took out a semicolon.

Any last ideas before I close this one out?
Thanks
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12165762
Thogek's correction was right.  Remember, you are writing JavaScript to the browser when you add an attribute like this.  The "==" is the equals operator for when you are evaluating an expression like:

if (this.value == '')

The "=" is the assignment operator when you want to assign the default text to the textbox:

this.value = '" + tx.Text + "';"

John
0
 

Author Comment

by:Bytes55
ID: 12165811
John,
I tried it without changing the "==" and it still does nothing.

But I used this (changing == to =):
tx.Attributes.Add("onfocus", "if(this.value='" + tx.Text + "')this.value='';")

and it worked.

0
 
LVL 15

Expert Comment

by:Thogek
ID: 12166600
Note: You may be using VB on the front-end, but this is client-side code, which is in JavaScript, in which == is used to compare values, and = is used only to assign them.
0
 
LVL 5

Assisted Solution

by:ajitanand
ajitanand earned 41 total points
ID: 12166603
I dont know what the fight is all about =)

Here is the code that works perfectly (soooooooooo simple):
-------------------------------------------------------------------------------------
tx.TextMode = TextBoxMode.MultiLine
tx.Text = "Add message here.  Click Add when done."
tx.Attributes.Add("onfocus", "if(this.value=='" + tx.Text + "') {this.value=='';}")
tx.Attributes.Add("onblur", "if(this.value=='') {this.value='" + tx.Text + "';}")
e.Item.Cells(3).Controls.Add(tx)
-------------------------------------------------------------------------------------

It throws out html similar to the following tag, upon execution:

<TEXTAREA NAME=".........."
onfocus="if(this.value=='Add message here.  Click Add when done.') {this.value='';}"
onblur="if(this.value=='') {this.value='Add message here.  Click Add when done.';}"
>Add message here.  Click Add when done.</TEXTAREA>

-----The onfocus and onblur are clientside javascript code.
-----In javascript, == is used for equality comparision, and = is used for assignment.


-----------------------
Clarificatio to what you wrote:
But I used this (changing == to =):
tx.Attributes.Add("onfocus", "if(this.value='" + tx.Text + "')this.value='';")
-----------------------
Actually in your code, the value is first set to tx.text through first assignment -
if(this.value='" + tx.Text + "')  --- Note the single = is the assignment operator in JS

And then your code sets it to a blank again with -
this.value=''
-----------------------
Although your code works, it is WRONG logically!




rgds,
Ajit Anand
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12166608
BTW, didn't you say that my code suggestion above (from 09/27/2004 04:11PM PDT) "worked perfect"?  (== and all?)
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12166613
ajitanand,
Your posted code looks an awful lot like what I've already suggested.  :-/
0
 
LVL 5

Expert Comment

by:ajitanand
ID: 12166631
Yes Thogek, you are right, this could be 'awful like' what you have suggested, but without any typos and errors, along with an explanation to the concept.

My intent here was to bring things up together in one place so that Bytes55 can understand it in one go without further confusions. enjoy buddy!

rgds,
Ajit Anand

0
 

Author Comment

by:Bytes55
ID: 12166842
ajitanand, thanks for the notes . . .
I tried your code exactly as it is and it didn't do anything.

Thogek, I will give you 125 points. I'll add some more points and
give the others guys some as well.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question