How to add tool tip for Dropdownlist Item in asp.net

In this post i am going to show you how to add tool tip for dropdownlist item in asp.net.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ToolTip_DropDownList.aspx.cs"
    Inherits="ToolTip_DropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlDemo" runat="server">
            <asp:ListItem Text="Asp.net" Value="ASP.NET, the next version of ASP, is a programming framework used to create enterprise-class Web Applications" />
            <asp:ListItem Text="C#" Value="C# (pronounced C-sharp) is an object-oriented programming language from Microsoft" />
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ToolTip_DropDownList: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //Check it is postback or not.
        if (!IsPostBack)
        {
            BindToolTip(ddlDemo);

        }
    }
    /// <summary>
    /// Use of this function is to bind title attribute
    /// to each element of ListControl.
    /// </summary>
    /// <param name="list"></param>
    private void BindToolTip(ListControl list)
    {
        foreach (ListItem item in list.Items)
        {
            item.Attributes.Add("title", item.Value);

        }
    }
}

See here the {{demo}}

1 comment: