g.pickardou
g.pickardou

Reputation: 35832

How to bind ASP TextBox's Text to its ToolTip?

I would like to the ToolTip always be the same as the ASP TextBox's Text. Of course I can write

AnyTextBox.Text = "any text";
AnyTextBox.ToolTip = "any text";

but I do not want to duplicate hundreds of assignment statements. I also could write change event handlers for the Text property, but I do not want to write dozens of event handlers just for this (if there is a more elegant solution)

Is there? Something like this:

<asp:TextBox ID="AnyTextBox" runat="server" ToolTip="binding magic goes here, but how?">

Thx in advance

Upvotes: 3

Views: 2784

Answers (6)

CodingIntrigue
CodingIntrigue

Reputation: 78525

You could write your own custom control which inherits from TextBox? I used the Text property to set the tooltip, but you can do it the other way around if you want.

Control:

public class TooltipTextBox : TextBox {
    public new string Text {
        get { return base.Text; }
        set
        {
            base.Text = value;
            this.ToolTip = value;
        }
    }
}

Markup:

<my:TooltipTextBox ID="AnyTextBox" runat="server" Text="binding magic goes here">

Upvotes: 3

Nikin Jasani
Nikin Jasani

Reputation: 51

Try using jquery

$(function () {

        var maybe = true;
        var text = $('.myTextBox').val();
        if (maybe) {
            $('.myTextBox').attr('title', text);
        }

    });

Upvotes: 0

Gitzerai
Gitzerai

Reputation: 184

That depends if you are looking for "live update" of tooltip = i.e. do you want to change the tooltip when the user changes the text on website? Then use "onchange" event of the input and JS function that would change its "title" attribute on each event call.

Or you want to ease of your server-side work and do not want to specify tooltip and text for each item, then go with custom control way (and I recommend RGraham code).

Or match both methods and use custom control that would also provide JS "refresh" code :-)

Upvotes: 0

pineconesundae
pineconesundae

Reputation: 353

I think using a JavaScript event handler will be your easiest solution. You don't need to write hundreds of event handlers though. Just write one and then use that same event handler for all your text boxes!

<asp:TextBox ID="AnyTextBox" runat="server" onchange="setTooltip(this)" />

And then the script would be

function setTooltip(textbox) {
    textbox.title = textbox.value;
}

That's it! Set that onchange event for all your Textboxes who you want to have this tooltip behavior.

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460098

As far as i know there is no such automatism. For what it's worth, maybe you can use PreRender:

protected void Page_PreRender(Object sender, EventArgs e)
{
    var allTextControlsWithTooltips = new List<WebControl>();
    var stack = new Stack<Control>(this.Controls.Cast<Control>());
    while (stack.Count > 0)
    {
        Control currentControl = stack.Pop();
        if (currentControl is WebControl && currentControl is ITextControl)
            allTextControlsWithTooltips.Add((WebControl)currentControl);
        foreach (Control control in currentControl.Controls)
            stack.Push(control);
    }
    foreach (var txt in allTextControlsWithTooltips)
        txt.ToolTip = ((ITextControl)txt).Text;
}

You could also use a UserControl which handles this event, then you just need to put it on all of the pages that should behave in this way. Or you could let all pages inherit from one base page.

Upvotes: 2

aguetat
aguetat

Reputation: 514

You can do this in the TextChanged event of the textbox :

<asp:TextBox ID="AnyTextBox" runat="server" TextChanged="AnyTextBoxTextChanged">

protected void AnyTextBoxTextChanged(object sender, EventArgs e)
{
this.AnyTextBox.Tooltip = this.AnyTextBox.Text;
}

Upvotes: 0

Related Questions