Solved

Clear text out when first clicked in Text Box in DataGrid

Posted on 2004-09-27
18
244 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

23 Experts available now in Live!

Get 1:1 Help Now