How to Add Tooltips to an ASP.NET Dropdownlist
By Peter Bromberg
A DropdownList can be enhanced with Tooltips for its items including the selected item.
Below is the codebehind for an ASP.NET Page with a DropdownList on it. Note that after the control is Databound, we add a title attribute for each item. We also add an onmouseover event
for the selected item.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DropDownListToolTips
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDdl();
}
}
protected void BindDdl()
{
List<string> items = new List<string>();
items.Add("One");
items.Add("Two and a half");
items.Add("Three");
items.Add("Four");
items.Add("One hundred Eighty Three");
ddl1.DataSource = items;
ddl1.DataBind();
foreach (ListItem _listItem in this.ddl1.Items)
{
_listItem.Attributes.Add("title", _listItem.Text);
}
// add a tooltip for the selected item also
ddl1.Attributes.Add("onmouseover", "this.title=this.options[this.selectedIndex].title");
}
}
}
How to Add Tooltips to an ASP.NET Dropdownlist (4154 Views)