Solved

Clear text out when first clicked in Text Box in DataGrid

Posted on 2004-09-27
18
247 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now