Monday, April 16, 2012

Show long text in Tooltip

As we all know, standard ToolTip property of ASP.NET server controls is having a limit of number of characters. Means if you have a long text of say, 10-12 lines, and some hundreds of words, the tooltip will be truncated in browser after a certain limit.

JQuery plugin is providing its own control to deal with this limit. But, in case, your project has not included JQuery libraries, you can still achieve this by writing some javascripts and attaching them in your controls.

For this, you first need to write javascript methods that will overwrite default behavior of tooltip. Technically speaking, you need to write methods for "onmouseover" and "onmouseout" respectively as follow:


function showCustomTooltip(hostControl) {
//hostControl = control where you want to override Tooltip
var tooltipText = hostControl.title;
var toolTipContainer = document.createElement('SPAN');
var textNode = document.createTextNode(tooltipText);
toolTipContainer.appendChild(textNode);
hostControl.parentNode.insertBefore(toolTipContainer, hostControl.nextSibling);
toolTipContainer.className = "customTooltipCss";
hostControl.title = "";
}
function hideCustomTooltip(hostControl) {
var controlText = hostControl.nextSibling.childNodes[0].nodeValue;
hostControl.parentNode.removeChild(hostControl.nextSibling);
hostControl.title = controlText;
}


next thing is to, attach this methods to the controls where you want to show tooltip. Say, the control name is "lblInfo"

protected void Page_Load(object sender, EventArgs e)
{
lblInfo.Attributes.Add("onmouseover", "showCustomTooltip(this)");
lblInfo.Attributes.Add("onmouseout", "hideCustomTooltip(this)");
}


Finally, as you can in the JavaScript code that I have assigned "className" property to "customTooltipCss" we need to add this class in CSS file (or whatever method you are using for accessing styles in your page.)


.customTooltipCss
{
position: absolute;
width: 400px;
margin: 1px;
padding: 2px;
background: #FFFFBB;
border: 1px solid lightgray;
}


That's all we need to do.
Please see screenshot to see how the tooltip appears while implemented using above method.

4 comments:

  1. //hostControl = control where you want to override Tooltip

    I want an asp:BoundField value (i have a grid view) to be the host control, how can i achieve that?

    ReplyDelete
  2. For that you need to first find the control.. I am not sure which is your base control (i.e GridView, ListView)..
    Do let me know with more details if you are unable to find the bound field control.. otherwise, after you get the instance of your control, you just need to bind it with javascript calls as shown in post's example

    ReplyDelete
  3. Hello,

    Works great and exactly what I am looking for. One point I noticed that it does not appear to be processing CRLFs (as with the standard internal tooltip functionality).

    Any suggestion on how to implement this with your solution (tooltip text to be displayed is stored and retreived from SQL database)?

    Thank you.

    ReplyDelete
  4. Over writing text form tool tip in Grid view label , how can over come this.

    ReplyDelete

Thanks for visiting my blog.
However, if this helped you in any way, please take a moment to write a comment.

Thanks
Nirman

Blog Archive