?
Solved

Clear text out when first clicked in Text Box in DataGrid

Posted on 2004-09-27
18
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
18 Comments
 
LVL 15

Accepted Solution

by:
Thogek earned 172 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 164 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
Independent Software Vendors: 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 164 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

Technology Partners: 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!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

801 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