Adding tooltips for Items in a dropdownlist control
Introduction
At times dropdownlists will contain data of more than anticipated length. However, no one prefers having dropdownlists which are very wide. Also, the 'ToolTipText' property of the dropdownlist will provide tooltip for the entire control. Meaning, TooTip is available only for the current selected item in the dropdownlist. But, requirement says that the tooltip should be available for each item in the dropdownlist.
Details
Here is a small tip thats pretty handy.
The AddToolTip is a generic routine that will accept an object of type dropdownlist. It would then inject javascript by making use of the 'title' attribute. The title attribute will be added to each of the items present in the object under discussion.
NOTE: This tip is applicable to IE 7.0 and later versions.
Implementation:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropDownList(bErrors); //Bind the dropdownlists.
}
AddToolTip(serverdropdownlist);
}
private void AddToolTip(DropDownList lst)
{
foreach (ListItem curItem in lst.Items)
{
curItem.Attributes.Add("title", curItem.Text);
}
}
Output:
Conclusion
Happy programming... :)