Search This Blog

Monday 16 February 2015

Show tooltip in Asp.net Dropdownlist items Mouse over Event in C#

Step 1 :- (Show tooltip.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Show tooltip.aspx.cs" Inherits="Demo.Show_tooltip" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Show tooltip in Asp.net Dropdownlist items Mouse over Event in C#</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <b>DropDownList</b>
            <br />
            <asp:DropDownList ID="DropDownList1" runat="server" Width="20%"></asp:DropDownList>
        </div>
    </form>
</body>

</html>

Step 2 :- (Show tooltip.aspx.cs)

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

namespace Demo
{
    public partial class Show_tooltip : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDropDownList();
            }
        }

        private DataTable GenerateData()
        {
            DataTable dt = new DataTable();

            DataRow dr = null;

            dt.Columns.Add(new DataColumn("RowNumber"typeof(string)));

            dt.Columns.Add(new DataColumn("Column1"typeof(string)));

            int totalRow = 50;

            for (int i = 0; i < totalRow; i++)
            {
                dr = dt.NewRow();

                dr["RowNumber"] = i + 1;

                dr["Column1"] = "What is My Row Number ?  Ans. - " + (i + 1);

                dt.Rows.Add(dr);
            }

            return dt;
        }

        public void BindDropDownList()
        {
            DropDownList1.DataSource = GenerateData();
            DropDownList1.DataTextField = "Column1";
            DropDownList1.DataValueField = "RowNumber";
            DropDownList1.DataBind();

            DropDownList1.Items.Insert(0, "-Select-");

            #region Using foreach Loop
            foreach (ListItem listitem in DropDownList1.Items)
            {
                listitem.Attributes["title"] = listitem.Text;
            }
            #endregion
        }
    }
}


Note :-

Altertnatively You can use the For Loop also to show tooltip in Asp.net DropDownList Control.

Example...

           #region Using for Loop
            for (int i = 0; i < DropDownList1.Items.Count; i++)
            {
                DropDownList1.Items[i].Attributes.Add("title", DropDownList1.Items[i].Text);
            }
            #endregion

No comments:

Post a Comment