Tuesday, November 27, 2012

How to add ToolTip for DropDownList Items?

  
ToolTip-

ToolTip is a small piece of information, which is used to show some idea or information about control and control's functionality.  This is most important when we are using icons for performing the operations. In every application it plays very important role either for web applications or windows applications.

Generally it is invisible but when we put mouse over any control then related ToolTip automatically gets visible.

In this article, I am explaining - How to add ToolTip for DropDownList Items?

Generally DropDownList doesn't have any direct property or method to set ToopTip for DropDowbList items; here I am giving simple code to add ToopTip for DropDownList items.

Write this code in Page_Load() event-

 protected void Page_Load(object sender, EventArgs e)
    {
        foreach (ListItem  item in DropDownList1.Items)
        {
            //Adding ToolTip for each item of DropDownList
              item.Attributes.Add("title", "item - "+ item.Text );
        }
    }


In above code, I have used foreach loop to iterate each item of DropDownList. Here insidse loop, I am using Attributes.Add() method to add title (ToopTip) to each item.
    
    //Adding ToolTip for each item of DropDownList
   item.Attributes.Add("title", "item - "+ item.Text );


 Here "title" is client side property of controls, using this property ToolTip can be added for any controls.  Using Attributes.Add() method we can also add any other client side method to any control.
 
The output of this code will be this this-


ToolTip

Thanks


No comments:

Post a Comment