Click here to Skip to main content
15,886,860 members
Articles / Web Development / HTML

Adding a tool tip to dropdown list and list box controls in Internet Explorer

Rate me:
Please Sign up or sign in to vote.
4.39/5 (27 votes)
1 Jun 2007CPOL2 min read 158.4K   927   33   52
Internet Explorer does not always show the full contents of dropdown lists and list boxes. This means if the data in the list box is bigger thatn the control's witdth, the user will not be able to fully see it. We can show a tooltip of each content to solve the problem.

Screenshot - tooltip.jpg

Introduction

As you can see in the above image, the rendering by Internet Explorer of the drop down / list box is not appropriate. It does not display the text which is out of the width of the control. To solve the problem, we can add a tooltip with the contents so that the user can read the full value.

Using the code

To solve the problem, I searched the Internet, but no appropriate solution was found. Although, some good third party controls were available which could display the contents using a combination of button, edit box, and div etc. But their management was also a problem. A solution that came into mind was to display the tool tip along with every element of the control so that the text of the non visible area could be viewed using the tool tip. The tool tip will be bound at server side and will work for both Firefox and Internet Explorer. Following is the code for binding a tool tip with a control.

C#
//
// This method binds tooltip with a dropdown/ any list control
//
public static void BindTooltip(ListControl lc)
{
    for (int i = 0; i < lc.Items.Count; i++)
    {
        lc.Items[i].Attributes.Add("title", lc.Items[i].Text);
    }
}

Here the "title" attribute is the magic which adds the tooltip with each list item.

All you have to do is to call this function after binding all the values with the control. You can save this code in your helper file and call wherever you need it. If you have more then one control in your page for which you want to attach the tool tip, you don't need to call this function many times but simply call another overload of it. This overload function will search the list boxes in the page and will attach the tool tip against every control that it will find.

C#
public static void BindTooltip(System.Web.UI.Page p)
{
    if (p == null || p.Form == null)
            return;
        BindTooltip(p.Form.Controls);
}

To make my work more easy, I just put the code in my master page. Every page that uses the master page will get the tooltip automatically as shown in the sample.

The complete source code is attached in the sample application.

Enjoy! Happy programming !

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
Mansoor Sarfraz works in a well reputed multinational software development company. Software development is not only his duty but his passion too. In his professional career he was Involved in the development of resource/content management system, workflow based systems, enterprise projects based on MS Windows DNA architecture and .NET framework, web based rich client UI development, Rest based backend web services and windows services for different systems. He was also involved in software designing and architecture. He has expertise in C#.NET, ASP.NET, Sql Server, Adobe Flex, Java, Ajax and JavaScript.

You can reach him through his blog
http://mansoorsarfraz.blogspot.com

Comments and Discussions

 
AnswerRe: Does not works for listbox inside the gridview control [modified] Pin
Mansoor Sarfraz10-Sep-08 20:32
Mansoor Sarfraz10-Sep-08 20:32 
GeneralNot quite the same - but quick and dirty workaround on IE 6 Pin
hground7-Aug-08 8:34
hground7-Aug-08 8:34 
GeneralThe same thing with Javascript Pin
ab_dc24-May-08 2:06
ab_dc24-May-08 2:06 
GeneralRe: The same thing with Javascript Pin
Madhu Rangarajan20-Jul-09 21:27
Madhu Rangarajan20-Jul-09 21:27 
Generaltooltip Pin
kartzferrari13-Mar-08 19:19
kartzferrari13-Mar-08 19:19 
QuestionRe: tooltip [modified] Pin
Mansoor Sarfraz13-Mar-08 19:37
Mansoor Sarfraz13-Mar-08 19:37 
GeneralVery good. Pin
george_v5-Sep-07 9:07
george_v5-Sep-07 9:07 
GeneralSolution for IE6 [modified] Pin
Mansoor Sarfraz16-Jul-07 20:09
Mansoor Sarfraz16-Jul-07 20:09 
IE6 doesn't support "title" attribute due to which it cannot show the tooltip.
But you can show the full value of selected item in drop down or list box using dhtml div. The solution will not show the value of individual items but it will show only the selected item value.

you may check the following links for more information and code

http://www.eggheadcafe.com/tutorials/aspnet/66fab5b5-1967-4282-b259-fb7ce24621c3/adding-a-tooltip-to-a-dro.aspx[^]

http://www.dynamicdrive.com/dynamicindex5/dhtmltooltip.htm[^]

Cheers Smile | :)
Regards
Mansoor Sarfraz

Business Development Manager
VEB Technologies
http://www.vebtech.com

GeneralRe: Solution for IE6 Pin
Madhu Rangarajan20-Jul-09 21:00
Madhu Rangarajan20-Jul-09 21:00 
GeneralIE6 Pin
josephbino13-Jul-07 0:02
josephbino13-Jul-07 0:02 
GeneralRe: IE6 Pin
Mansoor Sarfraz16-Jul-07 20:08
Mansoor Sarfraz16-Jul-07 20:08 
GeneralHelp. Pin
Moha19834-Jul-07 4:34
Moha19834-Jul-07 4:34 
Generaltooltip Pin
Bennyzhang22-Jun-07 19:30
Bennyzhang22-Jun-07 19:30 
GeneralRe: tooltip Pin
Mansoor Sarfraz24-Jun-07 18:32
Mansoor Sarfraz24-Jun-07 18:32 
GeneralRe: tooltip Pin
Bennyzhang24-Jun-07 19:02
Bennyzhang24-Jun-07 19:02 
GeneralRe: tooltip Pin
Mansoor Sarfraz25-Jun-07 19:08
Mansoor Sarfraz25-Jun-07 19:08 
GeneralRe: tooltip Pin
Bennyzhang26-Jun-07 13:47
Bennyzhang26-Jun-07 13:47 
GeneralRe: tooltip Pin
Mansoor Sarfraz26-Jun-07 19:17
Mansoor Sarfraz26-Jun-07 19:17 
GeneralRe: tooltip Pin
Bennyzhang26-Jun-07 19:27
Bennyzhang26-Jun-07 19:27 
GeneralRe: tooltip Pin
Bennyzhang26-Jun-07 19:29
Bennyzhang26-Jun-07 19:29 
GeneralRe: tooltip Pin
Mansoor Sarfraz27-Jun-07 18:21
Mansoor Sarfraz27-Jun-07 18:21 
GeneralRe: tooltip Pin
JeevaBharathi26-Aug-07 20:14
JeevaBharathi26-Aug-07 20:14 
AnswerRe: tooltip Pin
Mansoor Sarfraz27-Aug-07 18:27
Mansoor Sarfraz27-Aug-07 18:27 
QuestionRe: tooltip Pin
ysrcycle16-Jul-07 1:02
ysrcycle16-Jul-07 1:02 
GeneralOne Query... [modified] Pin
Blessy Arun6-Jun-07 19:09
Blessy Arun6-Jun-07 19:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.