ASP.NET: ListBox Tooltip

Posted Sun, Jan 31 2010 16:47 by Deborah Kurata

The ASP.NET ListBox does not display a horizontal scroll bar by default, which can be a problem if any of your list items are too long to fit. One solution to this problem is to use a tooltip. As the user moves the mouse over the ListBox entries, the full text appears in a tooltip.

image

The code to accomplish this is as follows:

In C#:

private void LB_PreRender(object sender, System.EventArgs e)
{
    foreach (ListItem item in LB.Items) {
        item.Attributes.Add("title", item.Text);
}

In C#, you also need to set up the event handler. In this example, the event handler is set up in the Page_Load event, but you could put it where it makes sense for your application.

LB.PreRender += LB_PreRender;

In VB:

Private Sub LB_PreRender(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles LB.PreRender
    For Each item As ListItem In LB.Items
        item.Attributes.Add("title", item.Text)
    Next
End Sub

The ListBox, named LB in this example, has a PreRender event. In the PreRender event the code loops through the ListBox items and sets the title attribute to the text of the item.

Use this technique any time you want to display a tooltip over your ListBox items.

Enjoy!

Filed under: , , , ,

Comments

# re: ASP.NET: ListBox Tooltip

Monday, February 08, 2010 10:37 AM by Jim Smith

Would you please show how to achieve in in markup as data-binding?

# re: ASP.NET: ListBox Tooltip

Tuesday, February 16, 2010 5:01 PM by Mike

This worked fantastically! Very elegant solution.

# re: ASP.NET: ListBox Tooltip

Thursday, March 25, 2010 2:46 AM by kamal

how can keep tooltip selected item from listbox

# re: ASP.NET: ListBox Tooltip

Thursday, April 15, 2010 4:19 AM by Toko BV

THIS IS GREAT!!!!!!

THNX

# re: ASP.NET: ListBox Tooltip

Wednesday, July 14, 2010 2:23 PM by Fernando Maciel

So good! tks from Brazil... :)

# re: ASP.NET: ListBox Tooltip

Tuesday, November 16, 2010 7:05 AM by Mahesh Vijayakumar

But i guess it works only when a datasource in  bound with it. How could we dispaly the tool tip for the same control when the data is loaded into it dynamically.. I may add or remove the items from the control at some point.

# re: ASP.NET: ListBox Tooltip

Friday, December 17, 2010 5:39 AM by Shankar

Gr8 Stuff...

Rating:*****

# re: ASP.NET: ListBox Tooltip

Monday, February 14, 2011 3:45 AM by koks

Can u share the ASP.Net code for this

# re: ASP.NET: ListBox Tooltip

Saturday, August 13, 2011 2:06 AM by Jailen

Hey, kliler job on that one you guys!

# re: ASP.NET: ListBox Tooltip

Wednesday, November 02, 2011 2:25 AM by Ankur

U r a legend mate!!

# re: ASP.NET: ListBox Tooltip

Thursday, December 29, 2011 3:55 PM by Tony G

I am fairly new to VB.NET and do not quite understand how to call this bit of code. I am unclear as to what parameters to pass it. Can anyone help? Also, i am interested in using it like Mahesh Vijayakumar who said, "But i guess it works only when a datasource in  bound with it. How could we dispaly the tool tip for the same control when the data is loaded into it dynamically.. I may add or remove the items from the control at some point. "

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: