Solved

Clear text out when first clicked in Text Box in DataGrid

Posted on 2004-09-27
18
250 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IIS Express Working in one Visual Studio Solution, but not in another. Why? 2 21
SQL Login 17 50
Code works but it's slow 28 70
performance issue in asp web 5 32
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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